Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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 admin中为具有公共引用模型的模型创建内联?_Django - Fatal编程技术网

如何在Django admin中为具有公共引用模型的模型创建内联?

如何在Django admin中为具有公共引用模型的模型创建内联?,django,Django,我有这样的模型: def Profile(models.Model): user = OneToOneField(User, primary_key=True) def OtherProfile(models.Model): user = OneToOneField(User, primary_key=True) def Item(models.Model): user = ForeignKey(User) 我希望在ProfileAdmin中有ItemInline(

我有这样的模型:

def Profile(models.Model):
    user = OneToOneField(User, primary_key=True)

def OtherProfile(models.Model):
    user = OneToOneField(User, primary_key=True)

def Item(models.Model):
    user = ForeignKey(User)
我希望在
ProfileAdmin
中有
ItemInline
(同样在
OtherProfileAdmin
中)。我试着这样做:

class ItemInline(admin.TabularInLine):
    model = Item

@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    inlines = [ItemInline]
但是,这向我显示了以下错误:

<class 'app.admin.ItemInline'>: (admin.E202) 'api.Item' has no ForeignKey to 'api.Profile'.
:(admin.E202)“api.Item”没有指向“api.Profile”的外键。
如何将
ItemInline
放入
ProfileAdmin


我无法将
ForeignKey(Profile)
放入
,因为这样我就无法从
OtherProfile
访问
,要能够内联添加,您必须创建一个FK(外键)

models.py

管理员


可能是因为您的
项目
模型引用了
用户
模型,而不是
配置文件
。抱歉,这是不可能的。我认为一个细节是不相关的,但显然是,有两种配置文件对象,
必须链接到
用户
,因此这两种配置文件都可以访问它。我编辑了这个问题来澄清这一点。
def Profile(models.Model):
    user = ForeignKey(User)

def Item(models.Model):
    user = ForeignKey(Profile)
class ItemInline(admin.TabularInLine):
    model = Item

@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    inlines = [ItemInline]