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.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/khHsVmf-ivwJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.