I am using django-treenode to structur certain data. That’s my model:
treenode_display_field = "name"
name = models.CharField(
Let’s assume, my tree looks like this (
id in parenthesis):
'Topic 1' (1)
'Topic 1.1' (2)
'Topic 1.2' (3)
'Topic 2' (4)
'Topic 2.1' (5)
'Topic 2.1.1' (6)
'Topic 2.2' (7)
'Topic 3' (8)
'Topic 3.1' (9)
'Topic 3.2' (10)
My goal is to cut out
'Topic 2' (4) and its descendants. I can filter for
tn_ancestors_pks, but that’s a string (!) containing the
pks, comma separated. So
excludes also other
pks like “14”, “42” and so on.
How can I filter for single ancestors?
If you’re saying that tn_ancestors_pks would have something like “4,5,6”, and you want to filter out the top-level topics, I’d think you could filter using startswith. e.g.
If it’s not the topmost-level you want to filter, then you could use
Side note: I know you’re probably too far along for this, but I generally recommend treebeard for managing hierarchical data within a database. It provides implementations of three of the basic mechanisms for efficiently storing hierarchies in relational databases, allowing you to optimize for your use-case. (We mostly rely upon the Materialized Path.)
Thank you, Ken, it works great with
startswith and the comma.
With that, django-treenode is good enough for now, but I will keep treebeard in mind for future work.