Re: Am I overlooking something or is this a bug in ModelForm?

2011-08-31 Thread koenb
On 31 aug, 17:12, Wim Feijen  wrote:
> Hello,
>
> I am using a form which (simplified) looks like this:
>
> class AdvancedSearchForm(forms.ModelForm):
>     email = forms.CharField(label='Emailadres', required=False)
>
>     class Meta:
>         model = Address
>         fields = [
>             'last_name',
>             'middle_name',
>             'first_name',
>             'email',
>             'street',
>         ]
>
> Because it is a search form, I do not want to validate the e-
> mailfield. However, whenever I add 'email' to get the order of the
> meta fields right, default email validation is used.
>
> I am very surprised at that. Why doesn't it honor my specified email
> field which is a CharField? It should get precedence, right?
>
> Am I overlooking something? Or is this a bug?
>
> For further reading, 
> see:https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overri...
> and:https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changi...

I do not think it is a bug. You are using a modelform, so it is normal
that model validation takes place. The fact that that field is not
validated when you leave it out of the fields list is because
otherwise partial forms would not be able to be validated (which was
needed for backwards compatibility).

It would seem more natural to use a normal form for a search form, and
not trigger model validation at all (since the goal of this form is
not creating a model instance)

If you really want to keep using a modelform here, you could reorder
the fields by setting the keyOrder attribute on the fields OrderedDict
in the forms __init__. I still think you would be better off switching
to a "normal" form here though.

K

-- 
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.



Am I overlooking something or is this a bug in ModelForm?

2011-08-31 Thread Wim Feijen
Hello,

I am using a form which (simplified) looks like this:

class AdvancedSearchForm(forms.ModelForm):
email = forms.CharField(label='Emailadres', required=False)

class Meta:
model = Address
fields = [
'last_name',
'middle_name',
'first_name',
'email',
'street',
]

Because it is a search form, I do not want to validate the e-
mailfield. However, whenever I add 'email' to get the order of the
meta fields right, default email validation is used.

I am very surprised at that. Why doesn't it honor my specified email
field which is a CharField? It should get precedence, right?

Am I overlooking something? Or is this a bug?

For further reading, see:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
and: 
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-order-of-fields

-- 
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.