Opened 2 years ago

Closed 17 months ago

Last modified 17 months ago

#21084 closed Bug (fixed)

Wrong resolution of content type id for proxy models in ModelAdmin

Reported by: alejandro Owned by: nobody
Component: contrib.admin Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

I have a number of proxy models that I want to present separately in the admin interface. A simplified example would be:

class Accommodation(models.Model):
    name = models.CharField(max_length=10)

class Inn(Accommodation):
    class Meta:
        proxy = True

class Hotel(Accommodation):
    class Meta:
        proxy = True


admin.site.register(Inn)
admin.site.register(Hotel)

However, due to how ModelAdmin gets the content type of the models the retrieved content type instances will always be those corresponding to Accommodation. This causes several problems:

  • The log entries shown in the admin index will have broken links because log entries have the content type id of Accommodation instead of Inn or Hotel.
  • In the change form, the content_type_id context variable will be the one of Accommodation content type, causing the "View on site" link not to work.

I'm not sure wether there are more cases where getting the content type from the concrete model breaks things in the admin. A naive fix would be to pass for_concrete_model=False for every ContentType.objects.get_for_model call in ModelAdmin code but I'm not aware of the consequences of that since I'm not familiar enough with Django internals.

Change History (8)

comment:1 Changed 23 months ago by timo

  • Has patch set
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Here is a PR for a duplicate ticket (#21260). Like the OP, I haven't look into whether or not this is the best fix and it also lacks tests.

comment:2 Changed 22 months ago by buremba

comment:3 Changed 22 months ago by bmispelon

  • Needs tests unset
  • Patch needs improvement set

As noted in the comments on github, the proposed pull request needs improvement.

comment:4 Changed 17 months ago by alejandro

I've written an updated patch and submitted a PR: https://github.com/django/django/pull/2518

comment:5 Changed 17 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 7a7f6fccae7470efc515ecd7efb917f5cf3f8b87:

Fixed #21084 -- Used proxy model's content type for admin log entries.

comment:6 Changed 17 months ago by Tim Graham <timograham@…>

In 5e74d6cf0a4fa0caa51e77af77d55c2572f0917d:

[1.7.x] Fixed #21084 -- Used proxy model's content type for admin log entries.

Backport of 7a7f6fccae from master

comment:7 Changed 17 months ago by Tim Graham <timograham@…>

In 3cba90f22a9120ebf9b00d1d074b8fb2152ecd2e:

Fixed #21084 -- Used proxy model's content type for admin log entries.

(the previous commit included only the test)

comment:8 Changed 17 months ago by Tim Graham <timograham@…>

In 32c857908998503cd595311eebc9256d44f65803:

[1.7.x] Fixed #21084 -- Used proxy model's content type for admin log entries.

(the previous commit included only the test)

Backport of 3cba90f22a from master

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