Karen, thanks for useful workaround.

Another (but little bit sick) workaround I see is to create separate
class for such field, and an optional OneToOneField to that class...

But I see as an ultimate solution to distinguish between NULL and
empty string for CharField and TextField, as I see it, here is reason
to have it.

NULL means undefined (or unknown) value, and its Python equivalent is
probably None.
´´ - empty string - means defined value (and that value is empty
string), and its Python equivalent is empty string.

We can found many many examples, where some entity has some parameter
defined and in that case it must be unique, or it is not defined at
all.

On Sep 9, 4:35 pm, Karen Tracey <[email protected]> wrote:
> On Wed, Sep 9, 2009 at 9:14 AM, Jan Ostrochovsky <[email protected]
>
> > wrote:
>
> > Hello,
>
> > I have the exact need, as author of this question:
>
> >http://stackoverflow.com/questions/454436/unique-fields-that-allow-nu...
> > .
>
> 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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to