Django 1.7 现在需要设置迁移。不幸的是,在默认字段值中使用 lambdas 或类似的东西会破坏这个过程。
我在模型中有以下内容:
def make_uuid(type):
"""Takes an entity type identifier string as input and returns a hex of UUID2 with a
type identifier pre-pended for readability"""
return str(type)+str(uuid.uuid1().hex)
class Accounts(models.Model):
"""Model representing Accounts"""
PENDING_STATUS = 0
ACTIVE_STATUS = 1
SUSPENDED_STATUS = 2
CANCELLED_STATUS = 3
BETA_STATUS = 4
STATUS_CHOICES = (
(PENDING_STATUS, 'Pending'),
(ACTIVE_STATUS, 'Active'),
(SUSPENDED_STATUS, 'Suspended'),
(CANCELLED_STATUS, 'Cancelled'),
(BETA_STATUS, 'Beta'),
)
account_name = models.CharField(max_length=255)
account_uuid = models.CharField(max_length=34, default=partial(make_uuid,'AC'), db_index=True, unique=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
updated_by = models.ForeignKey(User, null=True, blank=True)
status = models.IntegerField(max_length=2, choices=STATUS_CHOICES, default=PENDING_STATUS)
调用 partial 会中断迁移过程,并出现以下错误:
ValueError: Cannot serialize: <functools.partial object at 0x10e5cf9f0>
There are some values Django cannot serialize into migration files.
我仍然需要自动生成 UUID,所以有人知道不会破坏迁移的解决方法吗?