Wednesday, August 4, 2010

Re: Forms and 'prefix' parameter

On Aug 5, 2:23 pm, Karen Tracey <kmtra...@gmail.com> wrote:
> On Thu, Aug 5, 2010 at 12:10 AM, Ed Schofield <edschofi...@gmail.com> wrote:
> > Hi everyone,
>
> > I'm trying to use a view with multiple forms under Django 1.2.1. I'm
> > puzzled that the prefix parameter seems to screw up validation. Here's
> > a test case:
>
> > >>> from django import forms
>
> > >>> class MyForm(forms.Form):
> > >>>     field1 = forms.IntegerField(required=False)
> > >>>     field2 = forms.IntegerField()
>
> > >>> f1 = MyForm(data={'field1': 1, 'field2': 2})
> > >>> f2 = MyForm(data={'field1': 1, 'field2': 2}, prefix='p')
>
> > >>> f1.is_valid()
> > True
>
> > >>> f2.is_valid()
> > False
>
> > Can anyone explain why these are different?
>
> You need to include the prefix in the data dictionary keys for the form.
> See:http://code.djangoproject.com/ticket/13763#comment:3

I've done some more digging. It seems that the prefix on data
dictionary keys is required only when initializing the form, not when
accessing the form data:

>>> f1.cleaned_data
{'field1': 1, 'field2': 2}

>>> f2.cleaned_data
{'field1': 1, 'field2': 2}

rather than what I would now expect, given how the data argument is
processed:

{'p-field1': 1, 'p-field2': 2}

Isn't this oddly inconsistent?

-- Ed

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