djangocontenttypes

What are reverse generic relationships used for?


The documentation on contenttypes says this:

If you know which models you'll be using most often, you can also add a "reverse" generic relationship to enable an additional API.

But what's their practical use? I fail to understand.


Solution

  • If you have a model with:

    A) Kitchen ---generic relation---> any food
    

    But you know you will do often:

    B) Vegetables.get_kitchen()
    

    Instead of doing get_kitchen() manually, you can add the reverse relationship in the Vegetable model and it will get the Kitchen for you.

    This kind of reverse relation is automaticly added to the other model for OneToMany relation using ForeignKey because you know which models are going to be on the both sides of the relation.

    Since with generic relation, it can be any model, Django doesn't add the reverse relation for you. You have to specify it manually if you feel that you need it.