簡體   English   中英

django默認用戶的外鍵值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM