Django application for creating the tree structure in the model (very fast structure).
To create a new item in the tree need only choose a parent. The key (id) for the new item will be calculated automatically. You can get a list of the parents and the range of children without a database query.
Implemented to create a new item, delete the item, transfer item to the other parent (and the transfer of the group).
Description of the algorithm for constructing the tree can be found habr.ru or in the wiki (in Russian)
This app is available on PyPI.
Supports Django 1.8 and later on Python 2.7, 3.2 - 3.5.
Works only with PostgreSQL 9.1 and later!
Clone this repository. The is an example of a project (app myapp
use treensl
)
Change the settings for connecting to the correct PostgreSQL DB.
or:
pip install django-treensl
- Add app
treensl
insettings.py
- Create a new class of models from
Tree32Abstract
orTree64Abstract
(fromtreensl.models
). Add your field in the model (for their fields, do not use the following names:id
,parent_id
,lvl
,created_children
,removed_children
,holes
). If necessary, change the dimension of the default tree (propertiesLEVELS
andCHILDREN
). For example see myapp/models.py - Run
python manage.py makemigrations
- The new file migration, add the 3 lines as in the example file myapp/migrations/0001_initial.py (the rows are marked with comments
# add after makemigrations
)
Before executing the migrate
you can adjust the settings tree. The dimension of the tree depends on the length integer
.
Recommended values (in the format LEVELS/CHILDREN/ROOT_ID
):
- For int32:
3/1623/-2147483648
,4/255/-2147483648
,5/83/-2147483648
,6/39/-2147483648
- For int64:
3/2642243/-9223372036854775808
,4/65535/-9223372036854775808
,5/7129/-9223372036854775808
,6/1623/-9223372036854775808
,7/563/-9223372036854775808
,8/255/-9223372036854775808
,9/137/-9223372036854775808
,10/83/-9223372036854775808
Run python manage.py migrate
Now, to create a new item in the tree need only choose a parent. The key (id) for the new item will be calculated automatically.
You can get a list of the parents and the range of children without a database query.
For any id
call functions from the treensl.calc_values:
parents_list(id, LEVELS, CHILDREN, ROOT_ID)
- returns a list of the first to the last parentchildren_range(id, LEVELS, CHILDREN, ROOT_ID)
- returns a list of the range of children
LEVELS
, CHILDREN
, ROOT_ID
- settings current tree