2013-07-31 52 views
1

我改變了我以前的用戶模型,以便它現在繼承了django的用戶模型。django完整性錯誤,我知道爲什麼,但不知道如何解決

from django.contrib.auth.models import User 

class UserProfile(User): 
#fields.. 

但其他車型都指着我的前模特,現在如果我要遷移,我得到的錯誤:

(user_id)=(9) does not exist in auth_user table. 

合理的錯誤消息。但我現在應該做什麼?我真的被卡住了。我使用的Django 1.4版

我做出了錯誤的截圖:

enter image description here

回答

1

你不說你使用的是什麼版本的Django的;如果您使用的是1.5,那麼您還需要設置AUTH_USER_MODEL設置以告訴Django使用它(有關更多信息,請參閱auth docs)。如果您使用的是早期版本,則可能根本不想爲用戶模型進行子類化,但可以創建一個配置文件(如您的類名所示)作爲單獨的模型,並將其與ForeignKey關聯(有關更多信息,請參見old profile docs)。在那)。

當您添加父類時,您是否還更改了模型的名稱?您可能想要在UserProfile中設置表的名稱,以使其與舊名稱匹配。從Django model docs

To save you time, Django automatically derives the name of the database table from the name of your model class and the app that contains it. A model’s database table name is constructed by joining the model’s 「app label」 – the name you used in manage.py startapp – to the model’s class name, with an underscore between them.

For example, if you have an app bookstore (as created by manage.py startapp bookstore), a model defined as class Book will have a database table named bookstore_book.

To override the database table name, use the db_table parameter in class Meta.

所以這樣的事情會做的伎倆:

class UserProfile(User): 
    # other stuff 
    class Meta: 
     db_table = "myapp_user" 

希望這有助於!

+0

謝謝Paul,很有幫助。我認爲,繼承和放置外鍵是一回事。我正在使用django1.4 – doniyor

相關問題