Suppose I have several models representing objects of real life: "Man", "Chair", "Room"
I also have a Collection model, which is a collection of records from these models.
Each model can be a member more than in the collection, so I also created the Membership model, which represents the object, is a member of the collection. It is defined as follows:
class Membership(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
collection = models.ForeignKey('Collection', related_name="members")
I want to create a QuerySet that provides a collection that represents all of its members in this model. I know that I can do this programmatically, but I need it in a QuerySet, which can be filtered, ordered, etc.
EDIT:
Obviously, this can be done using raw SQL:
SELECT * FROM
( modelx INNER JOIN membership ON modelx.id = membership.object_id)
WHERE
( membership.collection_id=<my-collection-id> AND
membership.content_type_id=<modelx-type-id> )
But can it be represented using the Django query language?
adamk source
share