南迁移错误:未定义名称“UUID” [英] South migrate error: name 'UUID' is not defined

查看:359
本文介绍了南迁移错误:未定义名称“UUID”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有CharField字段的模型,默认值为uuid4:

I have a model with a CharField field with a default value of uuid4:

f = models.CharField(default=uuid4, max_length=36, unique=True, blank=True)

这是导致以下错误:


无法成功为模型
'm'创建字段'f':name'UUID'未定义。 p>

Cannot successfully create field 'f' for model 'm': name 'UUID' is not defined.

运行迁移的逗号!我可以解决这个问题吗?到目前为止,我试过:

running the migrate commmand! Ho can I fix this issue? so far I tried:


  • 在uuid的模块中定义一个包装函数(即:def getUUID())

  • 通过覆盖Model构造函数

...来设置默认值f问题仍然存在:(

...but the problem remains :(

ps。我知道我可以向南指示自定义字段,但我没有在我看来使用自定义字段:P

ps. I know that I can instruct south for custom fields, but I'm not using custom fields in my opinion :P

推荐答案

我解决了在我的模型模块中定义了以下帮助函数:

I solved defining the following helper function in my model's module:

from uuid import uuid4

def generateUUID():
    return str(uuid4())

然后:

f = models.CharField(default=generateUUID, max_length=36, unique=True, editable=False)

south将生成一个迁移文件(migrations.0001_initial )与生成的UUID,如:

south will generate a migration file (migrations.0001_initial) with a generated UUID like:

default='5c88ff72-def3-4842-8d48-a75bb3240bb5'

这是非常不开心的,因为该字符串是静态,而是必须使用帮助函数动态创建...无论如何在django的世界中似乎按预期工作...我在数据库中添加了一些记录,并为每个记录生成了一个新的UUID。然后我通过在我的模型中添加几个字段来尝试我的第一个模式迁移,并按预期的方式添加到数据库表中。

this is pretty unhappy... since that string is "static", instead it must be created dynamically using the helper function... anyway in the django's world al seems working as expected... I added some records into the database and a new UUID was generated for each one. I then tried my first schema migration by adding a couple of fields to my model and they has been added to the database table as expected.

这篇关于南迁移错误:未定义名称“UUID”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆