[英]django default foreign key value for users
我已经阅读了http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/,但仍然无法使用ForeignKey迁移我的新数据库模式对于产品列表中的用户
我的代码的简化版本是:
from django.contrib.auth.models import User
# other imports
class Product(models.Model):
author = models.ForeignKey(User)
# other product fields..
但是当我尝试迁移到南方时,这可能是问题所在,因为我的用户数据库不是由South处理的(原谅我的新手)我得到的错误是需要设置默认值:
_mysql_exceptions.OperationalError: (1067, "Invalid default value for 'author_id'")
谁能解释我做错了什么? 理想情况下,我不想要用户的默认值,或者至少为null,false或0(但这似乎不起作用)..
非常感谢,
亚当
如果您希望author
可以为空值,则需要使用null=True
定义ForeignKey。 您可能也希望blank=True
,因为它控制Django表单中的验证。
author = models.ForeignKey(User, null=True, blank=True)
结束了简单的路线并完全重置产品表,并从头开始迁移工作。 不知道我学到了什么(除了如何重置应用程序)!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.