Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django GenericTabularInline: (admin.E302) 'ct_field' references 'content_type', which is not a field

Tags:

django

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'.
like image 962
43Tesseracts Avatar asked Feb 09 '23 04:02

43Tesseracts


1 Answers

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"
like image 197
43Tesseracts Avatar answered May 20 '23 19:05

43Tesseracts