What are the drawbacks/risks with using Generic Relations?

From the Generic Relations docs

I stumbled across this ticket

Who’s first line is

(Yes, I know GFKs are evil and should be avoided at all costs. Nonetheless, as long as Django supports them, people will use them.)

So as someone new to Generic Relations, should they be considered an anti-pattern? Are there tradeoffs or risks to maintainability? If I have data that does model most directly to having a foreign key that points to multiple distinct Models, are there other options I should be considering instead?

<opinion>
Superficially, Yes.

More accurately, I’d put them in the category of “Limited and specific use”.

Yes, they’re useful. But, they shouldn’t be used as a general solution to a wide variety of situations. There are some very specific and targeted cases where they are the right answer. However, they’re frequently considered for use in situations where other solutions would be more appropriate.
</opinion>

For more details on this perspective, see the blog post at Avoid Django's GenericForeignKey - lukeplant.me.uk

I didn’t write it, but it says everything I would about the subject, plus some.

Regarding the alternives, there’s no “pat answer” for that. Deciding what solution should be used can only be made with a full understading of the complete system being modeled. It may even turn out that in your particular case, generic relations are the right answer.

1 Like