lundi 3 novembre 2014

Django 1.7 blank CharField/TextField convention


Vote count:

0




Using Django's new migration framework, let's say I have the following model that already exists in the database:



class TestModel(models.Model):
field_1 = models.CharField(max_length=20)


I now want to add a new TextField to the model, so it looks like this:



class TestModel(models.Model):
field_1 = models.CharField(max_length=20)
field_2 = models.TextField(blank=True)


When I try to migrate this model using python manage.py makemigrations, I get this prompt:



You are trying to add a non-nullable field 'field_2' to testmodel without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py


I can easily fix this by adding null=True to field_2, but Django's convention is to avoid using null on string-based fields like CharField and TextField (from http://ift.tt/1gVZ6fg). Is this a bug, or am I misunderstanding the docs?



asked 1 min ago







Django 1.7 blank CharField/TextField convention

Aucun commentaire:

Enregistrer un commentaire