1

I have a nested set category table developed with the PHP ORM Doctrine and I would like to port it to a Django app.

I started porting it to django-treebeard, but I am having difficulties and I am not sure it can work. The original table had the needed fields lft, rgt and depth, so I added the tree_id field.

I also had a foreign key to Accounts with one tree/account. Thus the table hold multiple independent trees that are not under a common root, with the lft and depth columns starting at 1 for each tree. So basically one nested set for each account in the table.

I can add nodes to a tree just fine, but when I call the get_last_child method, I get nodes from other accounts.

Does anyone know if there is a way to use treebeard, mptt or any other package without having to restructure the trees?

jphilip
  • 136
  • 2
  • 9
  • Q1: Did you use your existing database schema and create Django models using `manage.py inspectdb`? – jrief May 02 '20 at 10:21
  • Q2: Does the current tree representation for your category table contain a reference to the parent node? Then it would be possible to "repair" a nested sets tree using django-treebeard. – jrief May 02 '20 at 10:23
  • Yes to q1. I also had to to create and populate the tree_id field. – jphilip May 02 '20 at 15:21
  • Q2: The nested set is fine, actually in an experiment with mptt, I recreated a parent_Id field from the nested set. The overall set of trees displays fine, I am just having a hard time having the admin filtered to one root on a per account based on the account_id in the model. – jphilip May 02 '20 at 15:27

1 Answers1

0

I made some progress by adding the correct tree_id as a sequential number by account_id, which fixed some of the issues, with the query:

UPDATE category c,
(SELECT id, DENSE_RANK() OVER (ORDER BY account_id) AS  seq
FROM category ) tree_rank
SET c.tree_id = tree_rank.seq
WHERE c.id = tree_rank.id;

Now trying to get the admin to work.

jphilip
  • 136
  • 2
  • 9