Re: Validate uniqueness in forms

2008-12-30 Thread Karen Tracey
On Tue, Dec 30, 2008 at 7:22 AM, janedenone wrote:

>
> Hi,
>
> I was looking for a way to customize the error message when trying to
> submit an existing value for a unique field (using a ModelForm).
> According to this request (http://code.djangoproject.com/ticket/8913),
> I am not the first one to look for such a feature, and I'm happy to
> wait for it.
>
> My current solution is quite inelegant, though:
>
> if register_form.errors.has_key('email') and (register_form._errors
> ['email'] == ['Customer with this E-Mail already exists.']):
>register_form._errors['email'] = ErrorList(['My custom error message
> goes here.'])
>
> But I also found this snippet (http://www.djangosnippets.org/snippets/
> 1228/ ), and the first
> comment confused me. I never had to pass an
> instance when instantiating the form - the is_valid() method created
> the desired error message and linked it to the appropriate field.
>
> Am I missing something?
>
>
You need instance if you are updating an already-existing object.  (From
memory, so I could be getting this slightly wrong) Django's uniqueness code
is going to query the database to see if any existing row in the DB has a
value that matches one that is declared unique.  If you are editing an
already-existing object and have not changed the value of the unique field
in question, the answer will be "yes", since the object is already in the
DB, and that "yes" answer will lead to a ValidationError since it appears
the new value being provided is not unique in the DB.   When you pass
instance in during form creation, however, then that instance in the DB will
be excluded from the check, so the query becomes "does any row besides this
one that we are in the process of updating have this value in this
column?".  This query provides the correct answer for uniqueness check when
you are dealing with an already-existing row in the DB.

Karen

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



Validate uniqueness in forms

2008-12-30 Thread janedenone

Hi,

I was looking for a way to customize the error message when trying to
submit an existing value for a unique field (using a ModelForm).
According to this request (http://code.djangoproject.com/ticket/8913),
I am not the first one to look for such a feature, and I'm happy to
wait for it.

My current solution is quite inelegant, though:

if register_form.errors.has_key('email') and (register_form._errors
['email'] == ['Customer with this E-Mail already exists.']):
register_form._errors['email'] = ErrorList(['My custom error message
goes here.'])

But I also found this snippet (http://www.djangosnippets.org/snippets/
1228/), and the first comment confused me. I never had to pass an
instance when instantiating the form - the is_valid() method created
the desired error message and linked it to the appropriate field.

Am I missing something?

Kind regards,
Jan
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---