Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django generic foreign key and select_related

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!

like image 328
Clash Avatar asked Sep 24 '10 19:09

Clash


1 Answers

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!

like image 81
Bernhard Vallant Avatar answered Sep 19 '22 13:09

Bernhard Vallant