2013-07-31 101 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

相关问题