File: admin.rst

package info (click to toggle)
python-django-treebeard 5.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,048 kB
  • sloc: python: 5,271; javascript: 269; makefile: 178
file content (99 lines) | stat: -rw-r--r-- 2,609 bytes parent folder | download
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
Admin
=====

API
---

.. module:: treebeard.admin

.. autoclass:: TreeAdmin
   :show-inheritance:

   Example:

   .. code-block:: python

        from django.contrib import admin
        from treebeard.admin import TreeAdmin
        from treebeard.forms import movenodeform_factory
        from myproject.models import MyNode

        class MyAdmin(TreeAdmin):
            form = movenodeform_factory(MyNode)

        admin.site.register(MyNode, MyAdmin)


.. autofunction:: admin_factory


Interface
---------

The features of the admin interface will depend on the tree type.

Advanced Interface
~~~~~~~~~~~~~~~~~~

:doc:`Materialized Path <mp_tree>` and :doc:`Nested Sets <ns_tree>` trees have
an AJAX interface based on `FeinCMS`_, that includes features like
drag&drop and an attractive interface.

.. image:: _static/treebeard-admin-advanced.png

Basic Interface
~~~~~~~~~~~~~~~

:doc:`Adjacency List <al_tree>` trees have a basic admin interface.

.. image:: _static/treebeard-admin-basic.png


.. _FeinCMS: http://www.feincms.org

Model Detail Pages
~~~~~~~~~~~~~~~~~~

If a model's field values are modified, then it is necessary to add the fields 'treebeard_position' and 'treebeard_ref_node_id'. Otherwise, it is not possible to create instances of the model.

Example:

   .. code-block:: python

        class MyAdmin(TreeAdmin):
            list_display = ('title', 'body', 'is_edited', 'timestamp', 'treebeard_position', 'treebeard_ref_node_id',)
            form = movenodeform_factory(MyNode)

        admin.site.register(MyNode, MyAdmin)


Foreign keys and One-to-one relationships
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If your project contains models that have a foreign key or one-to-one relationship with a tree model,
you can leverage ``TreeNodeChoiceField`` to display the choices nicely in the Django admin. Given the following models:

    .. code-block:: python

        class TreeNode(MP_Node):
            ...

        class RelatedModel(models.Model):
            tree_node = models.ForeignKey("TreeNode")

You can configure the admin form for ``RelatedModel`` as follows for it to render the choices for ``tree_node`` in a nested list:

    .. code-block:: python

        class RelatedModelAdminForm(forms.ModelForm):
            tree_node = TreeNodeChoiceField(queryset=TreeNode.objects.all())

        class RelatedModelAdmin(admin.ModelAdmin):
            form = RelatedModelAdminForm

        admin.site.register(MyNode, MyAdmin)

.. warning::

   ``TreeNodeChoiceField`` should not be used with AL nodes, because they cannot be queried efficiently
   in this context.