Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating ManyToManyField to null true, blank true, isn't recognized

I have made a model change from

standard = models.ManyToManyField(Standard) 

to

standard = models.ManyToManyField(Standard, blank=True, null=True) 

South schemamigration for this app doesn't recognize the change?

Similar to this question, which is unanswered: South migrations and changes to many-to-may fields

like image 860
npradeetw Avatar asked Aug 14 '13 21:08

npradeetw


People also ask

Can many to many field be null?

model name: (fields. W340) null has no effect on ManyToManyField. null has no effect since there is no way to require a relationship at the database level.

How do I allow null values in Django model?

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.

How does Django handle null value?

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.

What is null true and blank true?

null=True , blank=True : This means that the field is optional in all circumstances. As noted below, though, this is not the recommended way to make string-based fields optional. null=False , blank=True : This means that the form doesn't require a value but the database does.


1 Answers

That behavior is correct: null doesn't mean anything at the database level when used with a ManyToManyField. The declaration of a ManyToManyField causes the creation of an intermediate table to hold the relationship, and although Django will create a standard attribute on your model instance for your convenience, there is no actual column representing it that could be nulled. By definition there can always be zero instances of the relationship.

blank=False, though, does have an effect on validation (when using model forms like the admin app, for example), forcing the user to choose at least one relation.

(Note that Django's built-in migration system creates migrations for just about any change to a model, regardless of whether it affects the database or not. So this change could lead to a migration, but it wouldn't affect the database or whether or not you could have zero instances of the relationship.)

like image 163
Kevin Christopher Henry Avatar answered Oct 04 '22 09:10

Kevin Christopher Henry