Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Does a new field in existing populated database need both: null=True, blank=True? [RSS Feed]

#1 June 13, 2010 22:47:59

a.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Does a new field in existing populated database need both: null=True, blank=True?


On 13 kesä, 12:14, Dave E <dever...@innotts.co.uk> wrote:
> I have a Django blog app with data in the (SQLite) database and have
> installed South so that I can add a 'modified' date field to the
> 'Entry' model. The field will initially be blank so I need blank=True
> for Django's admin validation.
>
> However, since it will also be empty in the database, do I need
> null=True as well (since it is a date field and not a string-based
> field)?
>
> If it were a string-based field, would I need to populate it with an
> empty string instead of null?
>
> I've read James Bennet's post and the Django docs (links below), but
> can't fathom the correct approach for this case:
>http://www.b-list.org/weblog/2006/jun/28/django-tips-difference-betwe...
>
>http://docs.djangoproject.com/en/dev/ref/models/fields/The difference between null=True and blank=True is as follows:
null=True means that the database will not check that the column has
some value other than null. blank=True is only for the admin app (and
IIRC ModelForms) meaning that the form validation will check that
there is some value other than None in the field. So, the first is for
database, the last for Django form validation.

If you don't define null=True the database will enforce that all rows
are not null. Of course when you add a field into the database, the
row will have a null value by default for the old rows, and the
database will give you an error.

If null values are Ok for the field, you should add null=True. If not,
you should add some default value for the field by running a command
something like "alter table foo add column bar timestamp not null
default '2009-01-01';" in the dbshell. As you can't know the modified
timestamp values for the old rows, I would probably allow null values
for the column, and the meaning of null would be 'not known'.

Anssi

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

Offline

#2 June 14, 2010 20:44:45

Dave E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Does a new field in existing populated database need both: null=True, blank=True?


Thanks Anssi - that's very clear - Dave

> If you don't define null=True the database will enforce that all rows
> are not null. Of course when you add a field into the database, the
> row will have a null value by default for the old rows, and the
> database will give you an error.
>
> If null values are Ok for the field, you should add null=True. If not,
> you should add some default value...

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

Offline

  • Root
  • » Django
  • » Does a new field in existing populated database need both: null=True, blank=True? [RSS Feed]

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support