Closed pfcodes closed 4 months ago
I was able to resolve the .select_subclasses()
issue by checking for the polymorphic subtype like this:
def resolve_creative(root: Model, info: strawberry.Info) -> Creative:
if hasattr(root.creative, "individualcreative"):
return root.creative.individualcreative
return root.creative.creativeteam
But I still haven't found a solution to nested fields still losing their prefetch cache when applying a filter. Closing this feature request since theres a workable solution, and opening an issue for the nested field filter problem.
Feature Request Type
Description
We use InheritanceManager from django-model-utils for polymorphism. To get the subclasses of a polymorphic model you need to call the special
.select_subclasses()
queryset method on the base class.I've defined an interface for the base class and types for the subclasses in Strawberry. The problem is the subclasses don't get downcasted because there's no way to call
.select_subclasses()
on the interface. This is different behavior than django-polymorphic which provides subclasses via the.all()
queryset method.As a result, I get this error message:
I've experimented with hacky workarounds like calling
.select_subclasses()
before hand in a prefetch as well as using the dataloader. But both break the optimizer and create n+1 queries for nested connection fields within the resolved subclass object that have a filter applied.This feature request is to request a way to somehow make polymorphism work with django-model-utils' InheritanceManager's
.select_subclasses()
. django-polymorphic was at one point abandoned and Strawberry should allow the leading alternative to be used.Upvote & Fund