1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
=============
Admin classes
=============
``mptt.admin.MPTTModelAdmin``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a bare-bones tree admin. All it does is enforce ordering, and indent the nodes
in the tree to make a pretty tree list view.
.. image:: mpttmodeladmin-genres.png
:align: center
:width: 26.21cm
:alt: MPTTModelAdmin screenshot
Usage::
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from myproject.myapp.models import Node
admin.site.register(Node, MPTTModelAdmin)
You can change the indent pixels per level globally by putting this in your
settings.py::
# default is 10 pixels
MPTT_ADMIN_LEVEL_INDENT = 20
If you'd like to specify the pixel amount per Model, define an ``mptt_level_indent``
attribute in your MPTTModelAdmin::
from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from myproject.myapp.models import Node
class CustomMPTTModelAdmin(MPTTModelAdmin):
# specify pixel amount for this ModelAdmin only:
mptt_level_indent = 20
admin.site.register(Node, CustomMPTTModelAdmin)
If you'd like to specify which field should be indented, add an ``mptt_indent_field``
to your MPTTModelAdmin::
# …
class CustomMPTTModelAdmin(MPTTModelAdmin):
mptt_indent_field = "some_node_field"
# …
``mptt.admin.DraggableMPTTAdmin``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 0.8.1
.. image:: draggablempttadmin-genres.png
:align: center
:width: 26.39cm
:alt: DraggableMPTTAdmin screenshot
This is a tree admin based on FeinCMS_ offering drag-drop functionality for
moving nodes::
from django.contrib import admin
from mptt.admin import DraggableMPTTAdmin
from myproject.myapp.models import Node
admin.site.register(
Node,
DraggableMPTTAdmin,
list_display=(
'tree_actions',
'indented_title',
# ...more fields if you feel like it...
),
list_display_links=(
'indented_title',
),
)
.. note::
Supported browsers include all recent versions of Firefox, Chrome,
Safari and Internet Explorer (9 or better).
.. warning::
Does not work well with big trees (more than a few hundred nodes, or trees
deeper than 10 levels). Patches implementing lazy-loading of deep trees
are very much appreciated.
It is recommended that ``tree_actions`` is the first value passed to
``list_display``; this also requires you to specify ``list_display_links``
because ``tree_actions`` cannot be used as the object link field.
``indented_title`` does nothing but return the indented self-description
of nodes, ``20px`` per level (or the value of ``mptt_level_indent``,
see below.)
``list_per_page`` is set to 2000 by default (which effectively disables
pagination for most trees).
You may set the attribute ``expand_tree_by_default = True`` in your
DraggableMPTTAdmin to expand the tree on first page visit (default is
False). After this the state of every node (expanded or collapsed) is saved
to the browser storage.
Replacing ``indented_title``
----------------------------
If you want to replace the ``indented_title`` method with your own, we
recommend using the following code::
from django.utils.html import format_html
class MyDraggableMPTTAdmin(DraggableMPTTAdmin):
list_display = ('tree_actions', 'something')
list_display_links = ('something',)
def something(self, instance):
return format_html(
'<div style="text-indent:{}px">{}</div>',
instance._mpttfield('level') * self.mptt_level_indent,
instance.name, # Or whatever you want to put here
)
something.short_description = _('something nice')
For changing the indentation per node, look below. Simply replacing
``indented_title`` is insufficient because the indentation also needs
to be communicated to the JavaScript code.
Overriding admin templates per app or model
-------------------------------------------
``DraggableMPTTAdmin`` uses the stock admin changelist template with some CSS
and JavaScript on top, so simply follow the official guide for
`overriding admin templates`_.
Changing the indentation of nodes
---------------------------------
Simply set ``mptt_level_indent`` to a different pixel value (defaults
to ``20``)::
# ...
class MyDraggableMPTTAdmin(DraggableMPTTAdmin):
mptt_level_indent = 50
# ...
.. _overriding admin templates: https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#overriding-admin-templates
.. _FeinCMS: https://github.com/feincms/feincms/
``mptt.admin.TreeRelatedFieldListFilter``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Admin filter class which filters models related to parent model with all it's descendants.
.. image:: treerelatedfieldlistlilter-genres.png
:align: center
:width: 26.21cm
:alt: MPTTModelAdmin screenshot
Usage::
from mptt.admin import TreeRelatedFieldListFilter
@admin.register(models.MyModel)
class MyModelAdmin(admin.ModelAdmin):
model = models.MyModel
list_filter =
(
('my_related_model', TreeRelatedFieldListFilter),
)
Changing the indentation of list filter nodes
---------------------------------------------
Simply set ``mptt_level_indent`` to a different pixel value (defaults
to ``10``)::
# ...
class MyTreeRelatedFieldListFilter(TreeRelatedFieldListFilter):
mptt_level_indent = 20
# ...
|