V
V
vfvnvsyevsky2015-03-06 07:18:43
Django
vfvnvsyevsky, 2015-03-06 07:18:43

How to store category structure in Django model?

Let's say there is a category that has supercategories and subcategories. How to organize the database so that you can easily query the supercategories and subcategories of a particular category?
Did it like this:

category_nested = models.ManyToManyField('self', symmetrical=False)

I write down only supercategories, but it is difficult to pull out subcategories. And django does not allow to do two ManyToManyFields:
ERRORS:
items.Category.category_nested: (fields.E304) Reverse accessor for 'Category.category_nested' clashes with reverse accessor for 'Category.category_parrent'.
HINT: Add or change a related_name argument to the definition for 'Category.category_nested' or 'Category.category_parrent'.
items.Category.category_parrent: (fields.E304) Reverse accessor for 'Category.category_parrent' clashes with reverse accessor for 'Category.category_nested'.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey K, 2015-03-06
@vfvnvsyevsky

https://github.com/django-mptt/django-mptt/

A
Alexey Cheremisin, 2015-03-06
@leahch

At the time, they also suffered. As a result, they began to store simply in the xlm text field with the structure. Pluses - very fast work with nesting by a simple xpath, minuses - there is no connection at the base level, but there are Sinhalese for this. Xml can be replaced with json.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question