简体   繁体   中英

Django: “Invalid default value” during “migrate” command

There is a simple model

class Baz(models.Model):
  FOO = ('foo', 'Foo')
  BAR = ('bar', 'Bar')
  FOO_BAR = (FOO, BAR)

  foo_bar = models.CharField(max_length=5, default=FOO, choices=FOO_BAR)

After making migrations via python manage.py makemigrations , when trying to migrate, getting error:

django.db.utils.OperationalError: (1067, "Invalid default value for 'foo_bar'")

Tried to put ('foo', 'Foo') explicitly with no result. Tried to use FOO_BAR[0] - same error.

Any ideas?

您不应将变量指定为默认值,而应提供将要保存的实际值:

foo_bar = models.CharField(max_length=5, default='foo', choices=FOO_BAR)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM