On Wed, Sep 9, 2009 at 9:14 AM, Jan Ostrochovsky <jan.ostrochov...@gmail.com
> wrote:

>
> Hello,
>
> I have the exact need, as author of this question:
>
> http://stackoverflow.com/questions/454436/unique-fields-that-allow-nulls-in-django
> .
>
>
I answered on that question because the answer that was there was incorrect.


> I want a value to be unique, if it is not NULL. Database backend
> allows such construction, but in Django admin I have got this error
> message when saving form: Business unit with this VAT id already
> exists.
>
> As I see it, Django admin is storing empty string (''), and not NULL,
> as documented here:
> http://docs.djangoproject.com/en/dev/ref/models/fields/#null,
> and that is the root cause - empty string is not NULL.
>
>
Correct, this is the problem.


> ----
>
> As I found later, this problem is (or was?) discussed also here:
> http://code.djangoproject.com/ticket/4136.
>
> Is there any progress in that area? (Some best practise howto...)
>
> Thanks in advance, any advice is welcome.
>
>
I don't know if that ticket will ever be fixed.  In general Django wants to
store an empty string, not NULL, for empty character fields.  That is not
going to change and nobody in that ticket has come up with a way to change
it just for this particular case.

You can work around the existing behavior in your own code by telling admin
to use a custom form with a custom clean method for the field in question
which turns the blank string into None.  Then NULLs will be stored in the
DB, and they willl not compare the same for uniqueness checks.

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

Reply via email to