gpt4 book ai didi

Django GenericTabularInline : (admin. E302) 'ct_field' 引用 'content_type' ,这不是一个字段

转载 作者:行者123 更新时间:2023-12-03 11:59:42 32 4
gpt4 key购买 nike

我试图让一个带有通用外键的模型 (Prereq) 出现在 MyModel 的管理员中。

模型.py:

class MyModel(models.Model):
name = models.CharField(max_length=50, unique=True)
#etc

class Prereq(models.Model):
parent_content_type = models.ForeignKey(ContentType, related_name='prereq_parent')
parent_object_id = models.PositiveIntegerField()
parent_object = GenericForeignKey("parent_content_type", "parent_object_id")

prereq_content_type = models.ForeignKey(ContentType, related_name='prereq_item')
prereq_object_id = models.PositiveIntegerField()
prereq_object = GenericForeignKey("prereq_content_type", "prereq_object_id")
prereq_invert = models.BooleanField(default=False, help_text = 'parent is available if user does NOT have this pre-requisite')

or_prereq_content_type = models.ForeignKey(ContentType, related_name='or_prereq_item', blank=True, null=True)
or_prereq_object_id = models.PositiveIntegerField(blank=True, null=True)
or_prereq_object = GenericForeignKey("or_prereq_content_type", "or_prereq_object_id")
or_prereq_invert = models.BooleanField(default=False, help_text = 'parent is available if user does NOT have this pre-requisite')`

管理.py:
class PrereqInline(GenericTabularInline):
model = Prereq
fk_name = "prereq_parent" #tried "parent_object" also

class MyModelAdmin(admin.ModelAdmin):
list_display = ('name', ...etc)
inlines = [
PrereqInline,
]

admin.site.register(MyModel, MyModelAdmin)

如何解决我收到的错误?
<class 'my_app.admin.PrereqInline'>: (admin.E302) 'ct_field' references 'content_type', which is not a field on 'my_app.Prereq'.

最佳答案

弄清楚了:

GenericTabularInline需要添加 ct_fieldct_fk_field因为这些字段没有使用默认名称 content_typeobject_id分别

class PrereqInline(GenericTabularInline):
model = Prereq
ct_field = "parent_content_type"
ct_fk_field = "parent_object_id"
fk_name = "parent_object"

关于Django GenericTabularInline : (admin. E302) 'ct_field' 引用 'content_type' ,这不是一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291170/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com