Ordering Problem with recursive method
See original GitHub issueHi!
I’ve a problem when execute TreeNode Model with nested list, the Tree isn´t respect the order iteration, for example:
FAKE_PAYLOAD = [
{ 'name': "LEGAL",
'children': [{ 'name': "CORPORATIVO",
'children': [{ 'name': "Sub-SubFolder 1" },
{ 'name': "Sub-SubFolder 2" },
{ 'name': "Sub-SubFolder 3" },
{ 'name': "Sub-SubFolder 4" },
{ 'name': "Sub-SubFolder 5" },
{ 'name': "Sub-SubFolder 6" },
{ 'name': "Sub-SubFolder 7" },
{ 'name': "Sub-SubFolder 8" },
{ 'name': "Sub-SubFolder 9" },
{ 'name': "Sub-SubFolder 10" },
{ 'name': "Sub-SubFolder 11" },
{ 'name': "Sub-SubFolder 12" },
{ 'name': "Sub-SubFolder 13" },
{ 'name': "Sub-SubFolder 14" },
{ 'name': "Sub-SubFolder 15" },
] },
{ 'name': "CONTRACTUAL",
'children': [
{ 'name': "Sub-SubFolder 1" },
{ 'name': "Sub-SubFolder 2" }] },
{ 'name': "GOBIERNO",
'children': [{ 'name': "Sub-SubFolder 1" }] }
] },
]
My model is like this:
class FakeFolder(TreeNodeModel):
name = models.CharField(max_length=255)
def __str__(self) -> str:
return self.name
My recursive method is:
def write_tree(tree_list, parent = None):
for folder in tree_list:
folder_obj = FakeFolder.objects.create(name=folder['name'])
if parent:
folder_obj.set_parent(parent)
if 'children' in folder:
write_tree(folder['children'], folder_obj)
After, executed get_children() of root node, the list it seems wrong order:
FakeFolder.objects.all()[0].children
[<FakeFolder: CONTRACTUAL>, <FakeFolder: CORPORATIVO>, <FakeFolder: GOBIERNO>]
And the order of children_pks is ‘106,90,109’, when the correct order must be maybe ‘90,106,109’ or:
[<FakeFolder: CORPORATIVO>, <FakeFolder: CONTRACTUAL>, <FakeFolder: GOBIERNO>]
With this package it’s possible to set order manually? I don’t see a method for this. The only way that I’ve found is with the priority property but only works in descendent order
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Recursive Programming - Towards Data Science
Finally, recall that knowing your ordering is the most important step to solving a recursive problem, and your aim is always to cover...
Read more >Recursive Bubble Sort - GeeksforGeeks
Background : Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong ......
Read more >Recursive Sorting Algorithms – Digilent Blog
Mergesort is a divide-and-conquer algorithm that divides an array of length n into n subarrays, and then recombines them using merge. Our ...
Read more >Examples of Recursion: Recursion in Sorting | SparkNotes
Recursive techniques can be utilized in sorting algorithms, allowing for the sorting of n elements in O(nlogn) time (compared with the O(n 2...
Read more >Recursion and Sorting - Learneroo
The key idea behind Quick Sort is to compare a list of elements to one element, partition the list to less-than and greater-than...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks, amm maybe if there are possible to order tree in based of custom property like a integer in ascending or descending order instead of alphabetically will be great. Because in the practice will need modify the order based on specific use case. Now I will override the get_children method to make possible different order…
I see behind of magic performance one weakness, for example if execute this on microservice or serveless, will always go to database because the memory location of cache is independently of any instance of docker, maybe a feature with redis will be great.
@aarondiazr just customize the
tn_priorityfield value, then executecls.update_tree().