[Answered ]-Django queries, generic content_types

2👍

No, it is the TaggedItem model that has a field of type ContentType, which is called content_type.

Each model has a primary key to which you refer by “pk”. Most of the time it IS the “id” field. But sometimes it is not.

In order to be consistent about it you can refer to id field as the pk.
So when you are saying filter(content_type__pk=… it is similar to filter(content_type__id…

The double underscore (__) means a reference to field of that model. You can keep stacking these:

Car.objects.filter(category__supercategory__name = "Nice Cars")

if you had a model Car with a foreign key to Category, which in turn had foreign key to SuperCategory which had a field named name.

Anyone correct me if I am wrong.

0👍

If you don’t understand Django’s double-underscore lookup notation, you need to read up on queries generally before delving into the generic relations. This is basic, and fundamental to all Django queries. Don’t try to run before you can walk.

Start with the documentation for lookups across relationships.

Leave a comment