-
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
5 changed files
with
79 additions
and
8 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,55 @@ | ||
How to show calculated fields on listview page? | ||
=========================================================== | ||
|
||
You have an admin for the :code:`Origin` model like this:: | ||
|
||
@admin.register(Origin) | ||
class OriginAdmin(admin.ModelAdmin): | ||
list_display = ("name",) | ||
|
||
|
||
Apart from the name, we also want to show the number of heroes and number of villains for each origin, which is not a DB field on :code:`Origin`. | ||
You can do this in two ways. | ||
|
||
|
||
Adding a method to the model | ||
=========================================================== | ||
|
||
You can add two methods to your :code:`Origin` model like this:: | ||
|
||
|
||
def hero_count(self,): | ||
return self.hero_set.count() | ||
|
||
def villain_count(self): | ||
return self.villain_set.count() | ||
|
||
And change :code"`list_display` to :code:`list_display = ("name", "hero_count", "villain_count")`. | ||
|
||
|
||
Adding a method to the ModelAdmin | ||
=========================================================== | ||
|
||
If you don't want to add method to the model, you can do instead add the method to the ModelAdmin. :: | ||
|
||
|
||
|
||
def hero_count(self, obj): | ||
return obj.hero_set.count() | ||
|
||
def villain_count(self, obj): | ||
return obj.villain_set.count() | ||
|
||
|
||
The :code:`list_display`, as earlier, changes to :code:`list_display = ("name", "hero_count", "villain_count")`. | ||
|
||
Performance considerations for calculated_fields | ||
=========================================================== | ||
|
||
With either of the above approaches, you would be running two exta queries per object (One per caluclated field). You can find how to optimize this in | ||
:doc:`optimize_queries`. | ||
|
||
|
||
With any of these changes your admin looks like this: | ||
|
||
.. 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
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