Opened 4 months ago

Closed 3 months ago

#32975 closed Bug (fixed)

ModelAdmin for proxy model with InlineModelAdmin for proxy superclass reference results in admin.E202

Reported by: Lucas Weyne Owned by: Taulant Aliraj
Component: contrib.admin Version: 3.1
Severity: Normal Keywords: proxy, InlineModelAdmin, E202
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Lucas Weyne)

This is similar to #30273, but in this case, the InlineModelAdmin.model is a model with references to a proxy superclass

Assume the following Django models:

class Reporter(models.Model):
    name = models.CharField(max_length=50)


class Journalist(Reporter):

    class Meta:
        proxy = True


class SpecialJournalist(Journalist):

    class Meta:
        proxy = True   


class Article(models.Model):
    journalist = models.ForeignKey(Journalist, on_delete=models.CASCADE)

Register model admins as follows (exemplary):

class ArticleInline(admin.TabularInline):
    model = Article
    fk_name = 'journalist'


@admin.register(SpecialJournalist)
class SpecialJournalistAdmin(admin.ModelAdmin):
    inlines = [ArticleInline]

This will result in the following error:

<class 'ArticleInline'>: (admin.E202) fk_name 'journalist' is not a ForeignKey to 'SpecialJournalist'.

This problem occurs on this check this check: https://github.com/django/django/blob/3.1.13/django/forms/models.py#L1006

A ValueError is raised because the result for SpecialJournalist._meta.get_parent_list() does not include Journalist:

>>> SpecialJournalist._meta.get_parent_list()
[<class 'Reporter'>]

Attachments (1)

test30273-modified.zip (18.6 KB) - added by Lucas Weyne 4 months ago.
Test project to see this error

Download all attachments as: .zip

Change History (11)

Changed 4 months ago by Lucas Weyne

Attachment: test30273-modified.zip added

Test project to see this error

comment:1 Changed 4 months ago by Lucas Weyne

Description: modified (diff)

comment:2 Changed 4 months ago by Lucas Weyne

Description: modified (diff)

comment:3 Changed 4 months ago by Mariusz Felisiak

Triage Stage: UnreviewedAccepted

Thanks for the report.

comment:4 Changed 4 months ago by Bal Krishna Jha

Owner: changed from nobody to Bal Krishna Jha
Status: newassigned

comment:5 Changed 4 months ago by Bal Krishna Jha

Owner: Bal Krishna Jha deleted
Status: assignednew

comment:6 Changed 3 months ago by Taulant Aliraj

Owner: set to Taulant Aliraj
Status: newassigned

comment:7 Changed 3 months ago by Taulant Aliraj

Has patch: set

comment:8 Changed 3 months ago by Mariusz Felisiak

Patch needs improvement: set

comment:9 Changed 3 months ago by Mariusz Felisiak

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:10 Changed 3 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 0e8be738:

Fixed #32975 -- Fixed admin system check for inlines with foreign keys to proxy models.

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