Tuesday, June 29, 2010

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

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

On Jun 29, 4:15 pm, rahul jain <jainwolver...@gmail.com> wrote:
> 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

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

No comments:

Post a Comment