0

I. Suppose you have the following model:

class Knight(models.Model):
    name = models.CharField(max_length=100)
    of_the_round_table = models.BooleanField()

II. Now you add this model to south's schemamigration and migrate:

python manage.py schemamigration myapp --initial
python manage.py migrate myapp 

III. You create an entry in the Knight table:

>>> jason = Knight.objects.get(name=”Jason”)

Nothing out of the ordinary here, just the regular routine. However notice the following.

IV-A. Add a new model field (CharField):

class Knight(models.Model):
    name = models.CharField(max_length=100)
    of_the_round_table = models.BooleanField()
    surname = models.CharField(max_length=100) # +

V-A. Write a schemamigration for this new field:

$ python manage.py schemamigration myapp --auto
 ? The field 'Knight.surname' does not have a default specified, yet is NOT NULL.
 ? Since you are adding this field, you MUST specify a default
 ? value to use for existing rows. Would you like to:
 ?  1. Quit now, and add a default to the field in models.py
 ?  2. Specify a one-off value to use for existing columns now

BUT

Now let's try step IV and V again but with a BooleanField instead of a CharField

IV-B. Add a new model field (BooleanField):

class Knight(models.Model):
    name = models.CharField(max_length=100)
    of_the_round_table = models.BooleanField()
    has_sword = models.BooleanField() # +

V-B. Write a schemamigration for this new field:

$ python manage.py schemamigration myapp --auto
 + Added field has_sword on registration.Knight
Created 0005_auto__add_field_knight_has_sword.py. You can now apply this migration with: ./manage.py migrate registration

Q1 : Why do I get a different output for adding a BooleanField(V-B) than for adding a CharField(V-A) even though according to the Django docs both these fields have null=False by default?

Q2 : How are you supposed to know which fields will give an output like BooleanField and which ones will give an output like CharField when you apply python manage.py schemamigration myapp --auto to a model in the given circumstance?

I'm using SQlite as RDBMS and Django 1.4 btw.

4

1 回答 1

3

这是因为在 Django 1.5 之前的版本中,BooleanFieldFalse被用作隐式默认值。这将在 Django 1.6 中改变

因此,虽然您CharField没有指定默认值,但强制 South 提示您,但BooleanField确实有默认值,即使是隐式的。

于 2013-10-08T10:45:19.163 回答