Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解决Django多表继承中涉及的表中的字段名冲突_Django_Django Models - Fatal编程技术网

解决Django多表继承中涉及的表中的字段名冲突

解决Django多表继承中涉及的表中的字段名冲突,django,django-models,Django,Django Models,我正在寻找解决以下模型中出现的字段名冲突的好方法: from django.db import models from django.contrib.auth.models import User class OauthProfile(models.Model): user = models.ForeignKey(User, null=True, blank=True, default=None) oauth_token = models.CharField(max_leng

我正在寻找解决以下模型中出现的字段名冲突的好方法:

from django.db import models
from django.contrib.auth.models import User

class OauthProfile(models.Model):

    user = models.ForeignKey(User, null=True, blank=True, default=None)
    oauth_token = models.CharField(max_length=200)
    oauth_secret = models.CharField(max_length=200)

class TwitterProfileInfo(OauthProfile):

    user_id = models.IntegerField(unique=True)
    screen_name = models.CharField(max_length=40, unique=True)
OauthProfile模型在appname_OauthProfile表中产生一个名为user_id的列,而TwitterProfileInfo模型(从OauthProfile继承)在appname_TwitterProfileInfo表中产生一个同名列。配置文件有意在根用户对象之前创建,因此我已将OauthProfile用户属性定义为允许NULL,默认值为None

在保存TwitterProfileInfo类型的对象时,没有将值分配给其用户属性或显式地将其分配给None,我希望数据库中的appname_oauthprofile.user_id保留为NULL。相反,查看PostgreSQL日志,我可以看到它被分配了我分配给对象的user_id属性的值。这会导致引用完整性错误,因为auth_user表中没有具有该值的行

myProfile = TwitterProfileInfo()
myProfile.user = None
myProfile.user_id = 12345 # Django tries to assign this to appname_oauthprofile.user_id
myProfile.screen_name = 'sometwitteruser'
myProfile.oauth_token = '1234567890abcdefg'
myProfile.oauth_secret = 'sometauthtokensecret'
myProfile.save() # results in error

我可以重命名其中一个冲突字段,例如,将TwitterProfileInfo字段名更改为类似twitter_user_id的名称。但我想知道是否有一种不这样做就能解决冲突的方法。如果我没有对数据库的控制权,并且被迫使用这些列名,我将如何定义这些模型以使其工作?

Django的多表继承就是这样一种反模式。它有各种可怕的后果,就像你注意到的那样

你的问题的答案是,不要使用继承。在
TwitterProfileInfo
OauthProfile
之间使用一个显式的OneToOneField。数据库结构完全相同,您可以完全控制如何引用字段。唯一的区别是,您需要分别实例化和保存
OauthProfile
对象,并显式设置关系:

my_oauth = OauthProfile(oauth_token='1234567890abcdefg', oauth_secret='sometauthtokensecret')
my_oauth.save()
my_twitter = TwitterProfileInfo(oauth_profile=my_oauth, user_id=12345)
my_twitter.save()

我不知道为什么我最初没有想到db_列选项。这很好,至少在本例中是这样:

class TwitterProfileInfo(OauthProfile):

    twitter_user_id = models.IntegerField(db_column='user_id', unique=True)
    screen_name = models.CharField(max_length=40, unique=True)
然后在其他地方

myProfile = TwitterProfileInfo()
myProfile.user = None
myProfile.twitter_user_id = 12345 # use the "custom" attribute name instead
myProfile.screen_name = 'sometwitteruser'
myProfile.oauth_token = '1234567890abcdefg'
myProfile.oauth_secret = 'sometauthtokensecret'
myProfile.save() # no error
尽管如此,我还是对Django的多表继承带来的负面影响感到好奇。如果它们很重要,那么我倾向于接受答案