Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for django-model-utils InheritanceManager via a custom queryset on interfaces #592

Closed
1 of 3 tasks
pfcodes opened this issue Jul 21, 2024 · 1 comment
Closed
1 of 3 tasks

Comments

@pfcodes
Copy link

pfcodes commented Jul 21, 2024

Feature Request Type

  • Core functionality
  • Alteration (enhancement/optimization) of existing feature(s)
  • New behavior

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:

Screenshot 2024-07-20 at 6 56 53 PM

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

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@pfcodes
Copy link
Author

pfcodes commented Jul 21, 2024

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.

@pfcodes pfcodes closed this as completed Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant