Friday, October 16, 2009

Re: validation error depending on widget

thanks a lot....

On Oct 16, 3:21 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Oct 16, 2:18 pm, wmstudio <henri.wittev...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > I have a model containing:
>
> > class Book(models.Model):
> >     title = models.CharField(max_length=128)
> >     price = models.DecimalField(max_digits=8, decimal_places=2,
> > help_text='Please enter the price in $')
> >     summary = models.TextField(blank=True)
>
> > and I adjust the textarea size using admin.py:
>
> > class BookModelForm(forms.ModelForm):
> >     title = forms.CharField(widget=forms.TextInput(attrs=
> > {'size':'80'}))
> >     summary = forms.CharField(widget=forms.widgets.Textarea(attrs=
> > {'rows':30, 'cols':100}))
> >     class Meta:
> >         Book
>
> > Now notice the model option "blank=True" for the summary field. When I
> > have an admin form and I enter the data but leave the "summary" field
> > empty I get a validation error, stating "This field is required".
> > However when I ommit the the summary entry in the admin class
> > BookModelForm like this:
>
> > class BookModelForm(forms.ModelForm):
> >     title = forms.CharField(widget=forms.TextInput(attrs=
> > {'size':'80'}))
> >     class Meta:
> >         Book
>
> > I am not the getting the validation error. Nothing changes in the
> > actual model. Am I missing something?
>
> When you override a field by redefining it in the modelform, you
> override everything, including its required/not required status. Since
> the default for form fields is for them to be required, you will get a
> validation error. You will need to specify required=False explicitly
> on the summary field if you override it.
> --
> DR.
--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate