J
J
Jekson2018-06-24 19:02:10
Django
Jekson, 2018-06-24 19:02:10

How do generic relationships work and what are they for in Django?

I learned that in Django, in addition to the main three types of relationships between models (MtM, OtO, OtM), there are also generalized relationships (GenericRelation). But I can't understand them. Ofdok read, it became a little better, but still I would be grateful if someone could describe in simple terms why they are needed, in what cases it is worth using and how they work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-06-24
@Lepilov

Suppose you have a news model and you can comment on it, for which the comment model is used. And after a month of operation of the site, the customer wanted more articles, and that they could also be commented on. And then all sorts of events - theatrical premieres, film screenings, concerts. And restaurant reviews loom ahead. That's just ForeignKey can only point to a specific model. All models will either have to inherit from a common root, or write a new comment model and all accompanying code for each commented model. Both are bad decisions. The way out is generalized relations that allow one model of comments to be used for an indefinitely large circle of previously unknown models.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question