Skip to content

Filtering normal model based on polymorphic relation #428

@niekvanderkooy

Description

@niekvanderkooy

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions