Skip to content

Commit

Permalink
Merge branch 'master' of github.com:agiliq/django-admin-cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Mar 7, 2018
2 parents 619b1f4 + 34f87c0 commit a5600f6
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/action_buttons.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ How to add Custom Action Buttons (not actions) to Django Admin list page?
UMSRA has decided that given sufficient kryptonite, all Heroes are mortal.
However, they want to be able to change their mind and say all heroes are immortal.

You have been absked to add two buttons - One which makes all heroes mortal, and one which makes all immortal. Since it affects all heores irrespective of the selection, this needs to be a separate button, not an action dropdown.
You have been asked to add two buttons - One which makes all heroes mortal, and one which makes all immortal. Since it affects all heroes irrespective of the selection, this needs to be a separate button, not an action dropdown.

First, we will change the template on the :code:`HeroAdmin` so we can add two buttons.::

Expand Down
4 changes: 2 additions & 2 deletions docs/add_actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ How to add additional actions in Django admin?
Django admin allows you to add additional actions which allow you to do bulk actions.
You have been asked to add an action which will mark multiple :code:`Hero` s as immortal.

You can do this by adding action a method to ModelAdmin
and adding the method as string to :code:`actions` ::
You can do this by adding the action as method to ModelAdmin
and adding the method as a string to :code:`actions` ::

actions = ["mark_immortal"]

Expand Down
2 changes: 1 addition & 1 deletion docs/boolean_fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Which looks like this

.. image:: filter_calculated_fixed.png

The :code:`is_very_benevolent` field show the string `True` and `False`, unlike the builin BooleanFields which show an on and off indicator.
The :code:`is_very_benevolent` field shows the string `True` and `False`, unlike the builtin BooleanFields which show an on and off indicator.
To fix this, you add a :code:`boolean` attribute on your method. You final modeladmin looks like this::

@admin.register(Hero)
Expand Down
2 changes: 1 addition & 1 deletion docs/custom_button.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Then you can override :code:`response_change` and connect your template to the :
if "_make-unique" in request.POST:
matching_names_except_this = self.get_queryset(request).filter(name=obj.name).exclude(pk=obj.id)
matching_names_except_this.delete()
obj.is_umique = True
obj.is_unique = True
obj.save()
self.message_user(request, "This villain is now unique")
return HttpResponseRedirect(".")
Expand Down
2 changes: 1 addition & 1 deletion docs/database_view.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You have a database view, created as this::


It has all the names from :code:`Hero` and :code:`Villain`. The id's for Villain are set to :code:`10000+id as id`
because we don't indent to cross 10000 Heroes::
because we don't intend to cross 10000 Heroes::

sqlite> select * from entities_entity;
1|Krishna
Expand Down
8 changes: 4 additions & 4 deletions docs/export.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ You have been asked to add ability to export :code:`Hero` and :code:`Villain` fr
There are a number of third party apps which allow doing this, but its quite easy without adding another dependency.
You will add an admin action to :code:`HeroAdmin` and :code:`VillanAdmin`.

An admin action always has this signature `def admin_action(modeladmin, request, queryset):`, alternatively you can add directly as a method on the :code:`ModelAdmin` like this::
An admin action always has this signature :code:`def admin_action(modeladmin, request, queryset):`, alternatively you can add it directly as a method on the :code:`ModelAdmin` like this::

class SomeModelAdmin(admin.ModelAdmin):

Expand All @@ -21,7 +21,7 @@ To add csv export to :code:`HeroAdmin` you can do something like this::

export_as_csv.short_description = "Export Selected"

This adds an action called export selected, which looks like this
This adds an action called export selected, which looks like this:

.. image:: export_selected.png

Expand All @@ -46,7 +46,7 @@ You will then change the :code:`export_as_csv` to this::

return response

This exports all the selected rows. If you notice, :code:`export_as_csv` doens't have anything specific to :code:`Hero`,
This exports all of the selected rows. If you notice, :code:`export_as_csv` doens't have anything specific to :code:`Hero`,
so you can extract the method to a mixin.

With the changes, your code looks like this::
Expand Down Expand Up @@ -85,4 +85,4 @@ With the changes, your code looks like this::
list_display = ("name", "category", "origin")
actions = ["export_as_csv"]

You can add such export to other models by subclassing from :code:`ExportCsvMixin`
You can add such an export to other models by subclassing from :code:`ExportCsvMixin`
2 changes: 1 addition & 1 deletion docs/filtering_calculated_fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ You have added filtering on the fields which come from the models, but you also

And then change your :code:`list_filter` to :code:`list_filter = ("is_immortal", "category", "origin", IsVeryBenevolentFilter)`.

With this you can filter on the calcualted field, and your admin looks like this:
With this you can filter on the calculated field, and your admin looks like this:

.. image:: filter_calculated_fixed.png
2 changes: 1 addition & 1 deletion docs/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ You have been asked to allow csv imports on the :code:`Hero` admin. You will do
)


Then you create the :code:`entities/heroes_changelist.html` templatee, by overriding the :code:`admin/change_list.html` template like this.::
Then you create the :code:`entities/heroes_changelist.html` template, by overriding the :code:`admin/change_list.html` template like this.::

{% extends 'admin/change_list.html' %}

Expand Down
2 changes: 1 addition & 1 deletion docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ who have some experience with Django admin, but are looking to expand their know
It takes the form of question and answers about common tasks you might do with Django admin. All the chapters are based on a common set of models, which you can read in detail here (:doc:`models`). In short, we have two apps,
:code:`events` and :code:`entities`. The models are

* Events: :code:`Epic`, :code:`Event`, :code:`EventHero`, :code:`EventVillian`
* Events: :code:`Epic`, :code:`Event`, :code:`EventHero`, :code:`EventVillain`
* Entities: :code:`Category`, :code:`Origin`, :code:`Hero`, :code:`Villain`


Expand Down
2 changes: 1 addition & 1 deletion docs/logo.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
How to add a logo to Django admin?
===========================================================

Your highers ups at UMSRA loge the admin you have created till now, but marketing wants to put the UMSRA logo on all admin pages.
Your higher ups at UMSRA love the admin you have created till now, but marketing wants to put the UMSRA logo on all admin pages.

You need to override the default templates provided by Django. In your django settings, you code::`TEMPLATES` setting looks like this. ::

Expand Down
4 changes: 2 additions & 2 deletions docs/optimize_queries.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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.
If you have a lot of calculated fields in your admin, you can be running multiple 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 :code:`ModelAdmin` we have for :code:`Origin`::
Expand All @@ -17,7 +17,7 @@ Lets take the example of this :code:`ModelAdmin` we have for :code:`Origin`::
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,
This adds 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::
Expand Down
6 changes: 3 additions & 3 deletions docs/sorting_calculated_fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ How to enable sorting on calculated fields?
===========================================================

Django adds sorting capabilities on fields which are attributes on the models.
When you add a calculated field Django doesn't know how to do a :code:`order_by`, so it doesn't a sorting capability on that field.
When you add a calculated field Django doesn't know how to do a :code:`order_by`, so it doesn't add sorting capability on that field.

If you want to add sorting on a calculated field, you have to tell Django, what to pass to :code:`order_by`. You can do this by setting
If you want to add sorting on a calculated field, you have to tell Django what to pass to :code:`order_by`. You can do this by setting the
:code:`admin_order_field` attribute on the calculated field method.

You start from the admin you wrote in the pervious chapter (:doc:`optimize_queries`).::
You start from the admin you wrote in the previous chapter (:doc:`optimize_queries`).::

hero_count.admin_order_field = '_hero_count'
villain_count.admin_order_field = '_villain_count'
Expand Down
2 changes: 1 addition & 1 deletion docs/two_admin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ And change the :code:`urls.py` to ::
]


This separates the admin. Noth admins are available at their respective urls, :code:`/entity-admin/` and :code:`event-admin/`.
This separates the admin. Both admins are available at their respective urls, :code:`/entity-admin/` and :code:`event-admin/`.

0 comments on commit a5600f6

Please sign in to comment.