我已通读过http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/ http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/但在使用外键为产品列表中的用户迁移新的数据库模式时仍然遇到问题
我的代码的简化版本是:
from django.contrib.auth.models import User
# other imports
class Product(models.Model):
author = models.ForeignKey(User)
# other product fields..
但是当我尝试迁移到南方时,这可能是问题所在,因为我的用户数据库不是由南方处理的(??原谅我的新手),我收到需要设置默认值的错误:
_mysql_exceptions.OperationalError: (1067, "Invalid default value for 'author_id'")
谁能解释我做错了什么?理想情况下,我不希望用户有默认值,或者至少是 null、false 或 0(但这似乎不起作用)。
非常感谢,
adam
如果您希望它可以有一个空值author
,您需要定义外键null=True
。你可能会想要blank=True
同样,它控制 Django 表单中的验证。
author = models.ForeignKey(User, null=True, blank=True)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)