views:

36

answers:

1

Hello guys, I'm trying to do select a model using a relation with a generic foreign key, but it's not working as expected.

I think it's better illustrated and understandable with code

class ModelA(models.Model):
 created = models.DateTimeField(auto_now_add=True)

class ModelB(models.Model):
 instanceA = models.ForeignKey(ModelA)

 content_type = models.ForeignKey(ContentType)
 object_id = models.PositiveIntegerField()
 content_object = generic.GenericForeignKey()

class ModelC(models.Model):
 number = models.PositiveIntegerField()
 bInstances = generic.GenericRelation(ModelB)

# Creating an instance of A and C
aInstance=ModelA.objects.create()
cInstance=ModelC.objects.create(number=3)

# Adding instance of C to the B_set of instance A
aInstance.modelb_set.add(content_object=cInstance)

# Select all ModelA instances that have C as content object? Does not work
whatIWant = ModelA.objects.filter(modelb__content_object=modelCInstance)

# Pseudo-solution, requires calling whatIWant.modelA
whatIWant = cInstance.bInstances.select_related("modelA") 

Just to be clear, I would like that this line worked: ModelA.objects.filter(modelb__content_object=modelCInstance), apparently django does not support using content_object on filter relationships.

Thanks in advance!

+2  A: 

Have a look at http://www.djangoproject.com/documentation/models/generic_relations/. And try:

ctype = ContentType.objects.get_for_model(modelCInstance)
what_you_want = ModelA.objects.filter(modelb__content_type__pk=ctype.id, 
                                      modelb__object_id=modelCInstance.pk)

Please have a look at some django coding/naming conventions, to make your code easier to read and understand!

lazerscience
Thanks for the tips! Is this the only way? I was looking for something more friendly, thanks anyways!
Clash
Since ContentTypes etc are not part of the django core the built-in `filter` doesn't know how to handle such queries, so you have to filter yourself for content_type AND the object_id!
lazerscience