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!