On Fri, Feb 4, 2011 at 3:31 PM, Marc Aymerich <glicer...@gmail.com> wrote: > On Fri, Feb 4, 2011 at 12:12 PM, Tom Evans <tevans...@googlemail.com> wrote: >> On Fri, Feb 4, 2011 at 11:07 AM, Marc Aymerich <glicer...@gmail.com> wrote: >>> Hi >>> In some choices widgets I want to represent null values as u'No >>> period' instead of u'--------' >>> >>> I try with: >>> >>> PERIOD_CHOICES = getattr(settings, 'PERIOD_CHOICES', >>> ((None, >>> ugettext('No Period')), >>> (DAILY, >>> ugettext('Daily')), >>> (MONTHLY, >>> ugettext('Monthly')), >>> (ANUAL, >>> ugettext('Anual')), >>> )) >>> period = models.CharField(max_length=1, >>> choices=settings.PERIOD_CHOICES, null=True) >>> >>> But when I choose "No Period" I get this form validation error: Value >>> u'None' is not a valid choice. >>> >>> Any idea? >>> Thanks! >> >> form.Field doesn't take null as an option, you want required[1]: >> >> """ >> By default, each Field class assumes the value is required, so if you >> pass an empty value -- either None or the empty string ("") -- then >> clean() will raise a ValidationError exception: >> """ >> >> Cheers >> >> Tom >> >> [1] http://docs.djangoproject.com/en/1.2/ref/forms/fields/#required > > > Hi Tom, > I override the default admin form with this: > > class MonitorAdminForm(ModelForm): > class Meta: > model = Monitor > period = forms.ChoiceField(required=False, choices = > settings.PERIOD_CHOICES) > > Now period has required=False but still raise the validation error: > Value u'None' is not a valid choice. > > What is strange for me is although I put the None value on > PERIOD_CHOICES the error seems to say that None is interpreted as a > unicode instead of None. Maybe this is the point, if it's, how can I > declare a null value in PERIOD_CHOICES definition ? >
Just I found the solution looking for others things :) help(admin.ModelAdmin) get_action_choices(self, request, default_choices=[('', '---------')]) On forms the null value is '' Thanks! -- Marc -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.