New CharField attribute to handle CharField(null=True, blank=True, unique=True) in model forms

465 views
Skip to first unread message

Jon Dufresne

unread,
May 18, 2016, 11:01:37 PM5/18/16
to django-d...@googlegroups.com
Hi,

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.

This choice to create the empty_value attribute API was based on the prior art of the empty_value attribute of TypedChoiceField [3].

In the PR Tim Graham requested I raise the new API on the developers list. I'm looking for concerns or feedback regarding the new attribute or any other issue with the PR.

Thanks!

Cheers,
Jon


[1] https://code.djangoproject.com/ticket/4136
[2] https://github.com/django/django/pull/6624
[3] https://github.com/django/django/blob/218175b/django/forms/fields.py#L832-L856

Claude Paroz

unread,
May 19, 2016, 3:03:52 AM5/19/16
to Django developers (Contributions to Django itself)
The design and the patch look good to me. Thanks!

Claude

Shai Berger

unread,
May 27, 2016, 9:17:57 AM5/27/16
to django-d...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages