I am trying to get a model (Prereq) with Generic Foreign Keys to appear in MyModel's admin.
models.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')`
Admin.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)
How to I solve the error I am getting?
<class 'my_app.admin.PrereqInline'>: (admin.E302) 'ct_field' references 'content_type', which is not a field on 'my_app.Prereq'.
Figured it out:
GenericTabularInline required the addition of ct_field
and ct_fk_field
because those fields did not use the default names of content_type
and object_id
respectively
class PrereqInline(GenericTabularInline):
model = Prereq
ct_field = "parent_content_type"
ct_fk_field = "parent_object_id"
fk_name = "parent_object"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With