Suppose I have a few models representing real life objects: "Person", "Chair", "Room"
I also have a "Collection" model, which represents some collection of records of these models.
Each model can be a member of more than on collection - therefore, I have also created a "Membership" model, which represents an object is a member of a 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 be able to create a QuerySet, which given a collection, represents all its members of a given model. I know I can do it 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?