Location>code7788 >text

Tree type storage and editing with django-treebeard

Popularity:467 ℃/2024-08-04 21:34:27

preamble

In fact, before doing a lot of projects have encountered with the tree-related features, used to be their own implementation, and then a lot of front-end UI component libraries have Tree components, set up on it can be used.

However, since the use of Django, or have to give full play to the advantages of the ecological, but I searched half a day, only this treebeard can be used, the other either do not stop changing or the function is very pull, no visual editing of the tree function.

Has Django fallen out of favor?

effect

Take this car modification program for example

The following effects are realized and can be edited by dragging the nodes

image

mounting

Installation of dependencies

pdm add django-treebeard

commander-in-chief (military)treebeard add toINSTALLED_APPS

Define the model

predecessorMP_Node The type can be

from treebeard.mp_tree import MP_Node

class CaseCategory(MP_Node):
    name = ('Category name', max_length=100)
    node_order_by = ['name']

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

    class Meta:
        db_table = 'car_case_category'
        verbose_name = 'Modification Category'
        verbose_name_plural = verbose_name

Configuring admin

Requires InheritanceTreeAdmin In order to visualize the tree editing

from  import TreeAdmin

@(CaseCategory)
class CaseCategoryAdmin(TreeAdmin):
    form = movenodeform_factory(CaseCategory)
    list_display = ['name', 'depth']
    search_fields = ['name']

Initialization data

The initialized Tree data can be imported using the code

(In fact because there isn't even an add button on the admin screen without importing the initialization data first ...... guess it's a bug)

def seed_data_treebeard().
    from import CaseCategory
    get = lambda node_id: (pk=node_id)
    root = CaseCategory.add_root(name='car coat')
    node = get().add_child(name='glossy/glossy')
    node = get().add_child(name='Frosted/Matte')
    root = CaseCategory.add_root(name='Color Change')
    get().add_child(name='Solid Colors')
    get().add_child(name='Gradient Color')
    get().add_child(name='Custom Colored')
    root = CaseCategory.add_root(name='Modification')
    get().add_child(name='Wheels')
    get().add_child(name='brakes')
    get().add_child(name='Shock')
    root = CaseCategory.add_root(name='Save Lift')

This opens the admin screen and you can see the

Sort of.

Write an interface.

Then I'll write a simple interface based on the django-ninja

(In fact, this code is automatically generated by DjangoStarter)

from typing import List
from  import get_object_or_404
from ninja import Router, ModelSchema
from django_starter. import responses

router = Router(tags=['case_category'])

class CaseCategoryOut(ModelSchema):
    class Meta:
        model = CaseCategory
        fields = ['id', 'path', 'depth', 'numchild', 'name', ]

@('/', response=List[CaseCategoryOut], url_name='car/case_category/list')
def list_items(request):
    qs = ()
    return qs

The resulting data came out like this (some data omitted)

{
  "code": 200,
  "data": [
    {
      "id": 4, "path": "0001", {
      "path": "0001",
      
      "numchild": 3,
      "name": "change color"
    },
    {
      "id": 7, "path": "00010001", {
      
      "depth": 2,
      "numchild": 0,
      "name": "custom coloring"
    },
    {
      "id": 6, "path": "00010002", {
      "path": "00010002", {
      "depth": 2,
      "numchild": 0,
      "name": "gradient color"
    }
  ]
}

wrap-up

It's better to be comfortable with your own realization.

But this is sort of out-of-the-box, so it's fine to just mess around with small projects.

bibliography

  • /