Dear Django group,
I've never worked with hierarchic database models before, and would be
very grateful for your advice:
In my application I need a hierarchy of objects, e.g.
class Account(models.Model):
parent = models.ForeignKey('self', null=True, blank=True)
name = models.CharField(max_length=60, blank=True)
In our application, there are only few instances of Account (about 150,
never changing much). So they would easily all fit into memory at once,
and we (possibly) have to traverse the entire hierarchy multiple times
for each view anyway.
My research and reading so far revealed that for modelling trees,
usually django-mptt or django-treebeard are suggested.
Would you still recommend them even for trees with small number of
instances as outlined above?
Compared to mptt, the above model looks appealingly lightweight and
simple to me, and my original plan was, for each view request, to
iterate once over Account.objects.all() in order to assemble the tree
"manually" as python objects in memory (with a reference to the related
Account instance at each node), then use that for all further processing.
Does that sound like good Django practice?
Then, doing further research, I found prefetch_related()
(https://docs.djangoproject.com/en/dev/ref/models/querysets/#prefetch-related)
Well, I have read this over and over again, but... is
Account.objects.all().prefetch_related('parent')
of any help here?
Given the recursive nature of Account, will
for a in Account.objects.all().prefetch_related('parent'):
print a.account_set.all()
perform better than
for a in Account.objects.all():
print a.account_set.all()
?
Any help or comments would much be appreciated!
Best regards,
Carsten
--
Cafu - the open-source Game and Graphics Engine
for multiplayer, cross-platform, real-time 3D Action
Learn more at http://www.cafu.de
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.