﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31553	Incorrect query for inline admin when the parent is a proxy model	Adam Duren	nobody	"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
}}}"	Uncategorized	closed	contrib.admin	3.0	Normal	needsinfo			Unreviewed	0	0	0	0	0	0
