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!
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With