Django 用户的默认外键值
我读过 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
I've had a read through http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/ but still am having problems migrating my new db schema with a ForeignKey for a user in a list of products
a simplified version of my code is:
from django.contrib.auth.models import User
# other imports
class Product(models.Model):
author = models.ForeignKey(User)
# other product fields..
but when I try to migrate to South, and this may be the problem, as my user database is not handled by South (??forgive my newbieness) i'm getting errors that a default value needs to be set:
_mysql_exceptions.OperationalError: (1067, "Invalid default value for 'author_id'")
Can anyone explain what i'm doing wrong?? Ideally I wouldn't want a default value for the user, or at least null, false or 0 (but that doesn't seem to work)..
many thanks,
adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望
author
可以具有空值,则需要使用null=True
定义ForeignKey。您可能还需要blank=True
,因为它控制 Django 表单中的验证。If you want it to be possible to have an empty value for
author
, you need to define the ForeignKey withnull=True
. You'll probably wantblank=True
as well, as that controls the validation in Django forms.最终采取了简单的路线并完全重置了产品表,并从头开始迁移,这是有效的。不确定我学到了什么(除了如何重置应用程序)!
Ended up taking the easy route and totally resetting the product table, and migrating from scratch which worked. Not sure i've learnt anything (other than how to reset an app)!