[Answered ]-How to filter by foreign key in GenericRelation?

1๐Ÿ‘

โœ…

If self.object is a Model3 instance, you could:

Model1.objects.filter(distributor_links__link=self.object)

This quiestion is similar to: Django โ€“ How to use a filter with a foreign key field?

That is, the name of the field, followed by a double underscore (__), followed by the name of the field in the new model, and so on for as many models as you want to join.

For more information: https://docs.djangoproject.com/en/dev/ref/models/querysets/

1๐Ÿ‘

Is it possible to resolve ContentType of object and use it and object id for filtering.

content_type = ContentType.objects.get_for_model(self.object)

Model1.objects.filter(distributor_links__content_type=content_type, distributor_links__object_id=self.object.id)

Leave a comment