生成UUID的默认值会破坏django 1.7中的迁移,是否有解决方法?

时间:2014-10-11 04:39:47

标签: django django-models

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)

部分调用会破坏迁移过程,并显示以下错误:

ValueError: Cannot serialize: <functools.partial object at 0x10e5cf9f0>
There are some values Django cannot serialize into migration files.

我仍然需要自动生成UUID,所以有人知道这种解决方法不会破坏迁移吗?

3 个答案:

答案 0 :(得分:7)

此处的问题是迁移系统需要序列化函数定义,并isn't able to do so使用partial()返回的动态创建的对象。

(注意as of version 1.9 Django实际上能够序列化partial()个callables,上面的代码可以工作。)

要解决此问题,请改用模块级函数:

def make_uuid_ac():
    return make_uuid('AC')

class Accounts(models.Model):
    ....
    account_uuid = models.CharField(..., default=make_uuid_ac)

答案 1 :(得分:2)

如果你需要将args传递给函数(即部分),docs中有解决方案:

from django.utils.deconstruct import deconstructible

@deconstructible
class MakeUUID(object):
    def __init__(self, pre):
        self.pre = pre

    def __call__(self, obj, *args):
        return '{}-{}-{}'.format(self.pre,
                                 obj.__class__.__name__,
                                 uuid.uuid1().hex)

make_uuid = MakeUUID('AC')

type作为参数名称也不是一个好主意。

答案 2 :(得分:-1)

重新阅读文档:https://docs.djangoproject.com/en/1.8/ref/models/fields/#uuidfield

请注意,可调用的(省略了括号)将传递给默认值,而不是UUID的实例。