Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django South - turning a null=True field into a null=False field

My question is, what is the best practice for turning a null=True field into a null=False field using Django South. Specifically, I'm working with a ForeignKey.

like image 515
orokusaki Avatar asked Jan 21 '11 16:01

orokusaki


People also ask

What is the difference between Null true and blank true in Django?

The fundamental difference between these two is that null controls the validation at the database level, and blank is used during form field validation at the application level. By default all fields are required. In order to make a field optional, we have to say so explicitly.

What is the difference between Null true and blank true?

null=True will set the field's value to NULL i.e., no data. It is basically for the databases column value. blank=True determines whether the field will be required in forms. This includes the admin and your own custom forms.

Is null false Django?

The Django convention is to use the empty string, not NULL. The default values of null and blank are False. Also there is a special case, when you need to accept NULL values for a BooleanField , use NullBooleanField instead.

What does blank true in Django mean?

If a string-based field has null=True , that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it's redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.


2 Answers

You should write first a data migration: http://south.aeracode.org/docs/tutorial/part3.html and then make the schemamigration.

like image 103
diegueus9 Avatar answered Nov 12 '22 12:11

diegueus9


If you want to turn nullable ForeignKey into non-nullable one, then it can be problematic if you have any rows with NULL for that field (column). In such case you need to either remove or fix them - possibly with custom data migration, like diegueus9 mentioned in the other answer.

But if you don't have any rows with NULL in that column, e.g. because you put that null=True only in case you might need it in the future, then you should be able to do a simple automatic schema migration:

$ ./manage.py schemamigration myapp remove_null_from_fkey --auto
(...)
$ ./manage.py migrate myapp
like image 44
Tomasz Zieliński Avatar answered Nov 12 '22 10:11

Tomasz Zieliński