Opened 3 years ago

Closed 3 years ago

#22034 closed Bug (fixed)

Checks for ModelAdmin ForeignKeys fail with GenericInlineModelAdmin

Reported by: Julian Wachholz Owned by: josven
Component: contrib.admin Version: master
Severity: Release blocker Keywords: checks
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Checks admin.E202 fail when trying to detect a ForeignKey from a GenericInlineModelAdmin:

The following setup yields this error:

<class 'app.admin.BarInline'>: (admin.E202) 'app.Bar' has no ForeignKey to 'app.Foo'.

class Foo(models.Model):
    """Foo may have some Bars."""
    text = models.TextField()

class Bar(models.Model):
    text = models.TextField()

    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

class BarInline(GenericStackedInline):
    model = Bar

class FooAdmin(admin.ModelAdmin):
    inlines = [BarInline]

I think this will require an exception in _check_fk_name for generic InlineAdmin classes here:

Change History (8)

comment:1 Changed 3 years ago by Marco Badan

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

I've 3 errors:

<class 'app.admin.BarInline'>: (admin.E202) 'app.Bar' has no ForeignKey to 'app.Foo'.
Bar.content_object: (contenttypes.E001) The field refers to "Bar.object_id" field which is missing.
Bar.content_object: (contenttypes.E005) The field refers to Bar.content_type field which is missing.

But object_id and content_type fields are in my database.

Note for @jwa: contenttypes.generic has been deprecated (

comment:2 Changed 3 years ago by Russell Keith-Magee

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

This is a newly added system check, so it's a release blocker.

comment:3 Changed 3 years ago by josven

Owner: changed from nobody to josven
Status: newassigned

These is a PR:
I'm introducing dependency to django.contrib.contenttypes.fields.GenericForeignKey.
Don't know if this is acceptable or not.

comment:4 Changed 3 years ago by josven

Changed the approach.

  • Created a GenericInlineModelAdminChecks that override def _check_fk_name.
  • Moved the dependency for django.contrib.contenttypes in django.conrtib.admin.checks, to having a dependency for django.contrib.admin.checks in django.contrib.contenttypes.checks instead.

comment:5 Changed 3 years ago by josven

The lastest approach did not completely work. The previous commit still works.

comment:6 Changed 3 years ago by Collin Anderson

I can confirm that the current pull request doesn't fix it but ​ works for me.

As far as the dependency goes, could we have a Inline._check_fk() on all inlines that lazy-imports code to handle the check?

Or, have a hook like Inline._checks = 'django.contrib.contenttypes.checks.GenericInlineModelAdminChecks'

As a side note, I like to do hacky things with my inlines (like store data in json on the original model) and sometimes that means it doesn't make sense to have an actual foreign key.

Version 0, edited 3 years ago by Collin Anderson (next)

comment:7 Changed 3 years ago by Russell Keith-Magee

The problem here is that GenericInlineModelAdmin is using the base InlineModelAdmin checks. We just need to make GenericInlineModelAdmin run it's own, generic-specific checks of the relation to the parent.

I'm working on a patch, incoming shortly.

comment:8 Changed 3 years ago by Russell Keith-Magee <russell@…>

Resolution: fixed
Status: assignedclosed

In 70ec4d776ef0e68960ccee21476b8654e9399f53:

Fixed #22034 -- Added a specific set of relation checks for GenericInlineModelAdmin.

Thanks to jwa for the report.

Note: See TracTickets for help on using tickets.
Back to Top