Hi,
On Thursday 19 May 2016 06:01:30 Jon Dufresne wrote:
>
> Occasionally I'll need to define a CharField on a model that is unique but
> also allow blank values. At the database level, this is easily handled by
> storing NULL for the blank values. (Storing the empty string multiple times
> will result in a DB unique constraint violation.) This use case has
> surfaced several times as evident by the 9 year old ticket #4136 [1].
>
> I have created a POC solution to the ticket at PR 6624 [2]. The change adds
> the attribute "empty_value" to forms.CharField. The value specified by
> empty_value is used as the Python empty value for the field. This value
> defaults to the empty string (current behavior) but could also be changed
> to None. The change also modifies model forms to set empty_value to None
> for CharField when null=True is set. The model forms change allows the use
> of the admin to save blank, unique char values without unique constraint
> violations.
>
I've added some comments on the PR. The TL;DR is that this causes some
problems for Oracle, where empty strings are always saved as nulls, but I
think we can solve most of the issues and document around the others.
Shai.