django-polymorphic icon indicating copy to clipboard operation
django-polymorphic copied to clipboard

Filtering normal model based on polymorphic relation

Open niekvanderkooy opened this issue 5 years ago • 1 comments

I'm sorry if this has been asked before, or has been documented somewhere, but I can't seem to find a way to do the following...

Say we have a NormalModel, which has a foreign key to a PolymorphicModel with children PolymorphicA and PolymorphicB. Is there a way to query NormalModel, based on the type of the polymorphic relation?

I would imagine something like NormalModel.objects.filter(Q(polymorphic_model___instance_of=PolymorphicA)), but since NormalModel has a normal manager, ___instance_of does not work. I could maybe hack around it with NormalModel.objects.filter(polymorphic_model__polymorphic_ctype_id=X) where I predetermine X, but I don't think that's how I should be doing things.

niekvanderkooy avatar Mar 12 '20 15:03 niekvanderkooy

I just realised I can do something like NormalModel.objects.filter(polymorphic_model__in=PolymorphicA.objects.all()), but this still feels like a bit of a workaround.

niekvanderkooy avatar Mar 12 '20 15:03 niekvanderkooy