File: tutorial.rst

package info (click to toggle)
python-django-treebeard 4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,024 kB
  • sloc: python: 4,940; makefile: 184
file content (110 lines) | stat: -rw-r--r-- 3,438 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
100
101
102
103
104
105
106
107
108
109
110
Tutorial
========

Create a basic model for your tree. In this example we'll use a Materialized Path tree:

.. code-block:: python

    from django.db import models
    from django.utils.encoding import python_2_unicode_compatible

    from treebeard.mp_tree import MP_Node

    @python_2_unicode_compatible
    class Category(MP_Node):
        name = models.CharField(max_length=30)

        node_order_by = ['name']

        def __str__(self):
            return 'Category: {}'.format(self.name)

This example works on Python 3 as well as on Python 2. If you don't need Python 2 support
remove the ``@python_2_unicode_compatible`` decorator.


Run syncdb:

.. code-block:: console

    $ python manage.py syncdb


Let's create some nodes:

.. code-block:: python

    >>> from treebeard_tutorial.models import Category
    >>> get = lambda node_id: Category.objects.get(pk=node_id)
    >>> root = Category.add_root(name='Computer Hardware')
    >>> node = get(root.pk).add_child(name='Memory')
    >>> get(node.pk).add_sibling(name='Hard Drives')
    <Category: Category: Hard Drives>
    >>> get(node.pk).add_sibling(name='SSD')
    <Category: Category: SSD>
    >>> get(node.pk).add_child(name='Desktop Memory')
    <Category: Category: Desktop Memory>
    >>> get(node.pk).add_child(name='Laptop Memory')
    <Category: Category: Laptop Memory>
    >>> get(node.pk).add_child(name='Server Memory')
    <Category: Category: Server Memory>

.. note::

    Why retrieving every node again after the first operation? Because
    ``django-treebeard`` uses raw queries for most write operations,
    and raw queries don't update the django objects of the db entries they
    modify. See: :doc:`caveats`.

We just created this tree:


.. digraph:: introduction_digraph

  "Computer Hardware";
  "Computer Hardware" -> "Hard Drives";
  "Computer Hardware" -> "Memory";
  "Memory" -> "Desktop Memory";
  "Memory" -> "Laptop Memory";
  "Memory" -> "Server Memory";
  "Computer Hardware" -> "SSD";


You can see the tree structure with code:

.. code-block:: python

    >>> Category.dump_bulk()
    [{'id': 1, 'data': {'name': u'Computer Hardware'},
      'children': [
         {'id': 3, 'data': {'name': u'Hard Drives'}},
         {'id': 2, 'data': {'name': u'Memory'},
          'children': [
             {'id': 5, 'data': {'name': u'Desktop Memory'}},
             {'id': 6, 'data': {'name': u'Laptop Memory'}},
             {'id': 7, 'data': {'name': u'Server Memory'}}]},
         {'id': 4, 'data': {'name': u'SSD'}}]}]
    >>> Category.get_annotated_list()
    [(<Category: Category: Computer Hardware>,
      {'close': [], 'level': 0, 'open': True}),
     (<Category: Category: Hard Drives>,
      {'close': [], 'level': 1, 'open': True}),
     (<Category: Category: Memory>,
      {'close': [], 'level': 1, 'open': False}),
     (<Category: Category: Desktop Memory>,
      {'close': [], 'level': 2, 'open': True}),
     (<Category: Category: Laptop Memory>,
      {'close': [], 'level': 2, 'open': False}),
     (<Category: Category: Server Memory>,
      {'close': [0], 'level': 2, 'open': False}),
     (<Category: Category: SSD>,
      {'close': [0, 1], 'level': 1, 'open': False})]



Read the :class:`treebeard.models.Node` API reference for detailed info.

.. _`treebeard mercurial repository`:
   http://code.tabo.pe/django-treebeard
.. _`latest treebeard version from PyPi`:
   http://pypi.python.org/pypi/django-treebeard/