Enable storing NULL in database

165 views
Skip to first unread message

Vlastimil Zíma

unread,
Nov 7, 2013, 5:37:47 AM11/7/13
to django-d...@googlegroups.com
Hi everybody,

I know this was here before, but it is really a problem. We need (I literally mean need, not just want) a simple way to store NULL in database instead of ''. Using empty string has several serious consequences (in databases which distinguishes between NULL and '', which are to my knowledge all but oracle).

 * You can't make a model with unique optional field. Django admin will let you store only one object without the value, because others with conflict the unique constraint.
 * It is useless to set NOT NULL constraints on text fields. Because Django stores empty string as empty value, database will not complain about invalid data and just stores the rubbish.

I don't want to enforce anybody to use only NULL as empty value, I just want Django to store NULL in database when I told it to. And I don't mean the way of overriding all the form fields as I have to now.

Regards,
Vlastimil

Elyézer Rezende

unread,
Nov 7, 2013, 7:57:03 AM11/7/13
to django-d...@googlegroups.com
Setting null=True in the field definition and doing model.field = None doesn't do this?

If you create a gist or a paste with a sample code would help understand better.


--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/f6641177-63a2-4adc-8166-503305c8e6b7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Elyézer Rezende
http://elyezer.com

Karen Tracey

unread,
Nov 7, 2013, 7:55:54 AM11/7/13
to django-d...@googlegroups.com
On Thu, Nov 7, 2013 at 5:37 AM, Vlastimil Zíma <vlas...@ziima.cz> wrote:
I don't want to enforce anybody to use only NULL as empty value, I just want Django to store NULL in database when I told it to. And I don't mean the way of overriding all the form fields as I have to now.

A concrete proposal would help here.

Aymeric Augustin

unread,
Nov 7, 2013, 7:59:37 AM11/7/13
to django-d...@googlegroups.com
2013/11/7 Elyézer Rezende <elye...@gmail.com>
Setting null=True in the field definition and doing model.field = None doesn't do this?

At the model layer, yes.

But if you're using a ModelForm — like the admin does — then you need some code to convert empty strings to None. I believe that's the problem Vlastimil wants to solve.

--
Aymeric.

Elyézer Rezende

unread,
Nov 7, 2013, 8:08:56 AM11/7/13
to django-d...@googlegroups.com
On Thu, Nov 7, 2013 at 10:59 AM, Aymeric Augustin <aymeric....@polytechnique.org> wrote:
But if you're using a ModelForm — like the admin does — then you need some code to convert empty strings to None. I believe that's the problem Vlastimil wants to solve.

Got it.

Thanks for the clarification. 

Vlastimil Zíma

unread,
Nov 8, 2013, 8:36:49 AM11/8/13
to django-d...@googlegroups.com
Augustin's correct :)

If I could suggest a solution, I would like to have a `null=False` argument on form fields. I believe this would fix the problems noted above and be sort of backwards-compatible. When using model forms, the argument would be set according to the `null` attribute of the respective model field.

Other version of this is a argument like `empty_value=''` which would allow more variability, though I'm not sure if it's needed.

I am not quite sure if there are completely unrelated places where Django uses hardcoded empty string as a empty value. That would be a problem.

What do you think?

Vlastimil

Dne čtvrtek, 7. listopadu 2013 13:55:54 UTC+1 Karen Tracey napsal(a):

Alessandro Pagnin

unread,
Nov 9, 2013, 4:49:40 AM11/9/13
to django-d...@googlegroups.com
I had the same problem to solve, and I made this app to solve the problem https://github.com/alepane21/django-nullablecharfield.
On the "widget level" it adds to the right of the field a checkbox to click to set the value to None/NULL.

Vlastimil Zíma

unread,
Nov 12, 2013, 3:21:09 AM11/12/13
to django-d...@googlegroups.com
Alesandro: Thanks for link, but this does not quite solve the issue.

1. I don't want user to decide how data gets stored in database. That's my (developer) decision.
2. There are other text fields which stores data as text in database - e.g. URLField and even the FileField.

Vlastimil

Dne sobota, 9. listopadu 2013 10:49:40 UTC+1 Alessandro Pagnin napsal(a):
Reply all
Reply to author
Forward
0 new messages