None field : Admin save setting it to empty value instead of NULL during save

683 views
Skip to first unread message

rahul jain

unread,
Jun 29, 2010, 11:15:44 AM6/29/10
to django...@googlegroups.com
Hi there !

One of my model fields attribute is set to null="true" to allow None
values. But if I use admin to save those model objects and leave it
blank, it saves blank value instead of None.
How to fix this ?

And Blank value is not None. Blank is "" (empty) but None is NULL in databases.

-RJ

euan.g...@gmail.com

unread,
Jun 29, 2010, 12:29:43 PM6/29/10
to Django users
Django admin can't differentiate between empty string and None and
picks empty string which makes sense in most cases.

If you don't like the idea of empty string override the save method
and coerce empty string to None before calling the super classes save,
e.g.

class YourModel(models.Model):
...

def save(self, *args, **kwargs):
if self.naughty_field == '':
self.naughty_field = None
super(YourModel, self).save(*args, **kwargs)

Euan

rahul jain

unread,
Jul 13, 2010, 5:44:21 PM7/13/10
to django...@googlegroups.com
Could it be generic ?. I mean i don't want to specify self.naughty_field.

How to do it for self.any_field ?

RJ

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

rahul jain

unread,
Jul 15, 2010, 3:10:37 PM7/15/10
to django...@googlegroups.com
anyone on this ?

Derek

unread,
Jul 15, 2010, 3:44:41 PM7/15/10
to django-users
Reply all
Reply to author
Forward
0 new messages