Django 用户的默认外键值

发布于 2024-10-14 03:27:09 字数 702 浏览 6 评论 0原文

我读过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-10-21 03:27:09

如果您希望author 可以具有空值,则需要使用null=True 定义ForeignKey。您可能还需要 blank=True,因为它控制 Django 表单中的验证。

author = models.ForeignKey(User, null=True, blank=True)

If you want it to be possible to have an empty value for author, you need to define the ForeignKey with null=True. You'll probably want blank=True as well, as that controls the validation in Django forms.

author = models.ForeignKey(User, null=True, blank=True)
一个人的旅程 2024-10-21 03:27:09

最终采取了简单的路线并完全重置了产品表,并从头开始迁移,这是有效的。不确定我学到了什么(除了如何重置应用程序)!

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)!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文