Checkbox becomes combox ... why ?

39 views
Skip to first unread message

yillkid

unread,
May 30, 2012, 5:24:53 AM5/30/12
to django...@googlegroups.com
Hi all.

I want to create a checkbox in my user register page, all of my code as below:

from django.utils.translation import ugettext as _
import sys
import datetime
reload(sys)
sys.setdefaultencoding('utf8')

from django.db import models
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from django.db.models.signals import post_save
from django.contrib import admin

from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.admin import widgets

from datetime import datetime

from django.contrib.admin.filterspecs import FilterSpec, DateFieldFilterSpec
from django.utils.translation import ugettext as _
from datetime import datetime

from django.forms import ModelMultipleChoiceField

TYPES = (
        ('yes', 'Yes'),
        ('no', 'No'),
        ('maybe', 'Maybe'),
    )

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    timestamp = models.DateTimeField(_("timestamp"))
    credit = models.DecimalField(_("credit"), max_digits = 8, decimal_places = 1)
    ipaddress = models.IPAddressField()
    nickname = models.CharField(_("nickname"), max_length = 8)
    game_type = models.CharField(choices = TYPES, max_length = 12)

class UserProfileInline(admin.StackedInline):
        model = UserProfile

class UserAdmin(UserAdmin):
    inlines = [UserProfileInline,]
    list_display = ('username', 'nickname', 'credit', 'leave', 'is_active', 'date_join', 'timestamp', 'ipaddress')
    list_filter = ('is_active',)

    def credit(self, instance):
        return instance.get_profile().credit

    def timestamp(self, instance):
        return instance.get_profile().timestamp.strftime('%Y/%m/%d %I:%H')

    def ipaddress(self, instance):
        return instance.get_profile().ipaddress

    def nickname(self, instance):
        return  instance.get_profile().nickname

    def leave(self, instance):
        return instance.get_profile().credit - 1

    def date_join(self, instance):
        return instance.date_joined.strftime('%Y/%m/%d %I:%H')   

    credit.short_description = _("credit")
    nickname.short_description = _("nickname")
    timestamp.short_description = _("timestamp")
    leave.short_description = _("leave")
    date_join.short_description =_("date_join")

class RegisterForm(UserCreationForm):
    timestamp = forms.DateTimeField(required=True, label=_("timestamp"), widget=widgets.AdminDateWidget())
    credit = forms.DecimalField(label=_("credit"))
    nickname = forms.CharField(label=_("nickname"))
    game_type = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple(), choices = TYPES)

    class Meta:
        model = User
        fields = ("username", _("timestamp"), _("credit"), _("nickname"), _("date_join"), _("game_type"))

    def save(self, commit=True):
        user = super(RegisterForm, self).save(commit=False)
        user.timestamp = self.cleaned_data["timestamp"]
        user.credit = self.cleaned_data["credit"]
        user.nickname = self.cleaned_data["nickname"]
        if commit:
            user.save()
        return user

admin.site.unregister(User)
admin.site.register(User, UserAdmin)



Filed "game_type" is a check box widget, BUT when I render the register page,
It doesn't render to a checkbot but a combobox.
I don't why, thanks for reading my problem.
Message has been deleted

yillkid

unread,
May 30, 2012, 11:14:24 PM5/30/12
to django...@googlegroups.com
Thanks, but not main problems.
Even if I remove "maybe" items,
same incorrect outputs.

Dennis Lee Bieber於 2012年5月31日星期四UTC+8上午12時09分44秒寫道:
On Wed, 30 May 2012 02:24:53 -0700 (PDT), yillkid <yil...@gmail.com>
declaimed the following in gmane.comp.python.django.user:

>
> Filed "game_type" is a check box widget, BUT when I render the register
> page,
> It doesn't render to a checkbot but a combobox.
> I don't why, thanks for reading my problem.

        Off-hand... Checkboxes are two-state items: On/Off (Yes/No; 1/0;
checked/unchecked). BUT you are providing a three-state value for it --
how is a checkbox supposed to render "Maybe"?
--
        Wulfraed                 Dennis Lee Bieber         AF6VN
        wlf...@ix.netcom.com    HTTP://wlfraed.home.netcom.com/  
Reply all
Reply to author
Forward
0 new messages