[Django] #31553: Incorrect query for inline admin when the parent is a proxy model

6 views
Skip to first unread message

Django

unread,
May 8, 2020, 2:46:26 PM5/8/20
to django-...@googlegroups.com
#31553: Incorrect query for inline admin when the parent is a proxy model
-----------------------------------------+------------------------
Reporter: Adam Duren | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.admin | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
In our application we have a `ModelAdmin` which displays all rows and a
special `ModelAdmin` which displays a subset of data available on the
table (filtered rows) via the use of ProxyModel. The `ModelAdmin` contains
an inline to a related model.

We noticed that as our related table grew the time it took to render the
one using a proxy model increased drastically. The non-proxy model detail
page loads in sub-second where as the proxy model detail page takes over a
minute.

Upon further inspection we discovered through the use of `django-debug-
toolbar` that the query which was being issued for the proxy model did not
filter the related objects by the foreign key but instead retrieves all
objects from the database.

Below is an example which will reproduce the issue. You can easily verify
by creating two users. Create 100K notifications and assign them to User
A. Click on the detail for User B and observe that User B takes just as
long as User A to load only when using the Proxy model.

{{{#!python
# models.py
class User(models.Model):
name = models.CharField()

class UserProxy(User)
class Meta:
proxy = True

class Notification(models.Model):
user = models.ForeignKey("Person", models.CASCADE)
text = models.CharField()

# admin.py
class NotificationAdmin(admin.TabularInline):
model = Notification

"""
Query for will be:

SELECT *
FROM "app_notification"
WHERE app_notification"."user_id" = 'd0b44931-2d8d-4668-a74b-
38b83ba9abf4'::uuid
"""
class UserAdmin(admin.ModelAdmin):
model = User

"""
Query for will be:

SELECT *
FROM "app_notification"
"""
class UserProxyAdmin(admin.ModelAdmin):
model = UserProxy
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31553>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 13, 2020, 3:11:47 AM5/13/20
to django-...@googlegroups.com
#31553: Incorrect query for inline admin when the parent is a proxy model
-------------------------------+--------------------------------------

Reporter: Adam Duren | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.admin | Version: 3.0
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => needsinfo


Comment:

Can you upload a sample project showing this in full please?

>... instead retrieves all objects from the database.

I suspect that this is because it's rendering the select widget for the
form, which is the expected bahaviour if you're not using an autocomplete
or raw_id field.

--
Ticket URL: <https://code.djangoproject.com/ticket/31553#comment:1>

Reply all
Reply to author
Forward
0 new messages