[Fixed]-Django many-to-many generic relationship

16👍

Your approach is exactly the right way to do it given your existing tables. While there’s nothing official (this discussion, involving a core developer in 2007, appears not to have gone anywhere), I did find this blog post which takes the same approach (and offers it in a third-party library), and there’s also a popular answer here which is similar, except only one side of the relationship is generic.

I’d say the reason this functionality has never made it into django’s trunk is that while it’s a rare requirement, it’s fairly easy to implement using the existing tools. Also, the chance of wanting a custom “through” table is probably quite high so most end-user implementations are going to involve a bit of custom code anyway.

The only other potentially simpler approach would be to have base Participant and Reward models, with the ManyToMany relationship between those, and then use multi-table inheritance to extend these models as Member/Friend etc.

Ultimately, you’ll just need to weigh up the complexity of a generic relation versus that of having your object’s data spread across two models.

👤Greg

9👍

Late reply, but I found this conversation when looking for a way to implement generic m2m relations and felt my 2 cents would be helpful for future googlers.

As Greg says, the approach you chose is a good way to do it.

However, I would not qualify generic many to many as ‘easy to implement using existing tools’ when you want to use features such as reverse relations or prefetching.

The 3rd party app django-genericm2m is nice but has several shortcomings in my opinion (the fact that the ‘through’ objects are all in the same database table by default and that you don’t have ‘add’ / ‘remove’ methods – and therefore bulk add/remove).

With that in view, because I needed something to implement generic many-to-many relations ‘the django way’ and also because I wanted to learn a little bit about django internals, I recently released django-gm2m. It has a very similar API to django’s built-in GenericForeignKey and ManyToManyField (with prefetching, through models …) and adds deletion behavior customisation. The only thing it lacks for the moment is a suitable django admin interface.

👤ouk

Leave a comment