Opened 11 years ago

Closed 11 years ago

Last modified 11 years 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 by Tim Graham, 11 years ago

Has patch: set
Needs tests: set
Triage Stage: UnreviewedAccepted

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:3 by Baptiste Mispelon, 11 years ago

Needs tests: unset
Patch needs improvement: set

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

comment:4 by alejandro, 11 years ago

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

comment:5 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 7a7f6fccae7470efc515ecd7efb917f5cf3f8b87:

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

comment:6 by Tim Graham <timograham@…>, 11 years ago

In 5e74d6cf0a4fa0caa51e77af77d55c2572f0917d:

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

Backport of 7a7f6fccae from master

comment:7 by Tim Graham <timograham@…>, 11 years ago

In 3cba90f22a9120ebf9b00d1d074b8fb2152ecd2e:

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

(the previous commit included only the test)

comment:8 by Tim Graham <timograham@…>, 11 years ago

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