-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,47 @@ | ||
How to optimize queries in Django admin? | ||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
If you have a lot of calculated fields in your admin, you can be running multipel queries per object leading to your admin can becoming quite slow. | ||
To fix this you can override the :code:`get_queryset` method on model admin to annotate the calculated fields. | ||
|
||
Lets take the example of this `ModelAdmin` we have for `Origin`:: | ||
|
||
@admin.register(Origin) | ||
class OriginAdmin(admin.ModelAdmin): | ||
list_display = ("name", "hero_count", "villain_count") | ||
|
||
def hero_count(self, obj): | ||
return obj.hero_set.count() | ||
|
||
def villain_count(self, obj): | ||
return obj.villain_set.count() | ||
|
||
|
||
This add two extra queries per row in your listview page. To fix this you can override the :code:`get_queryset` to annotate the counted fields, | ||
and then use the annotated fields in your ModelAdmin methods. | ||
|
||
With the changes, your ModelAdmin field looks like this:: | ||
|
||
|
||
@admin.register(Origin) | ||
class OriginAdmin(admin.ModelAdmin): | ||
list_display = ("name", "hero_count", "villain_count") | ||
|
||
def get_queryset(self, request): | ||
queryset = super().get_queryset(request) | ||
queryset = queryset.annotate( | ||
_hero_count=Count("hero", distinct=True), | ||
_villain_count=Count("villain", distinct=True), | ||
) | ||
return queryset | ||
|
||
def hero_count(self, obj): | ||
return obj._hero_count | ||
|
||
def villain_count(self, obj): | ||
return obj._villain_count | ||
|
||
There are no per object extra queries. Your admin continues to look like it did before the :code:`annotate` call. | ||
|
||
.. image:: calculated_field.png | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters