Some modelling advice

There are a number of different ways to store a “tree structure” or “hierarchy” within a database. There’s a package called django-treebeard that we use for this purpose. It implements three of the most common algorithms. It’s an interesting topic of study in that each of the three have different performance characteristics regarding searching, inserting, and deleting nodes within the tree. (This particular implementation where you have an FK to self is the Adjacency List.)

For some other reading on the topic, I can suggest Managing Hierarchical Data in MySQL — Mike Hillyer's Personal Webspace
(I thought I had one more link, but I can’t find the reference at the moment.)

I’ve been back and forth, up and down, and circled the wagons any number of times about GFKs. I’ve both recommended for and recommended against them at nearly the same time. I’m of the general mindset that they do have value and can be used quite effectively, but the number of such cases where they’re really useful is a lot smaller than you might think. (See Avoid Django's GenericForeignKey - lukeplant.me.uk for some reasons why.)

Cheers!

1 Like