In the article on
creating a system of bookmarks
on Django, an example was considered with the use of an abstract model for several types of bookmarks, namely for articles and comments on articles. Attention was also drawn to the fact that the model fields that had foreign keys to different models should have the same names to support the possibility of creating a single interface for adding bookmarks. This is made possible by the so-called "duck typing", which implies that objects that do not have a single inheritance hierarchy can be used in the same scenario if there are interfaces (methods) that have the same signature.
Literally the principle of duck typing sounds like this:
If it looks like a duck, swims like a duck and quacks like a duck, then it probably is a duck.
That is, having methods with the same signature, we can use objects that are not associated with an inheritance hierarchy, in the same context.
In the same article, let's consider the variant when using the Like Dislike system, not two different tables for articles and comments are used, and not even one that will contain a foreign key for an article or comment (that is, two columns, One of the columns, depending on what type of content the user activity is related to), and one table that will contain:
- The content type to which the record belongs
- Record ID
- The generated foreign key to write, in fact the content object
Other additional fields