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

get_queryset() is not called #488

Open
alimony opened this issue Feb 23, 2024 · 5 comments
Open

get_queryset() is not called #488

alimony opened this issue Feb 23, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@alimony
Copy link
Contributor

alimony commented Feb 23, 2024

I'm trying to add a .get_queryset() class method to one of my types, but it is never called when querying. I just put a print on top of the method and it never happens. Fields on the type behave normally so I know the type is in use.

I can't get even the simple example from the docs to work:

@strawberry_django.type(models.Fruit)
class Berry:

    @classmethod
    def get_queryset(cls, queryset, info, **kwargs):
        return queryset.filter(name__contains="berry")

What could be going on here?

I'm on version 0.28.2.

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
@alimony alimony added the bug Something isn't working label Feb 23, 2024
@bellini666
Copy link
Contributor

Hi @alimony ,

Can you provide a MRE or at least give more information about how you are using using that type? Because there are some differences between using it directly on a root type (e.g. Query) or nested inside another type.

@alimony
Copy link
Contributor Author

alimony commented Feb 23, 2024

It looks something like this:

@strawberry_django.type(
    Customer,
    filters=CustomerFilter,
    fields=[
        "name",
        ...
    ],
)
class CustomerType:
    name: auto
    org: OrgType

    @classmethod
    def get_queryset(
        cls, queryset: QuerySet[Customer], info: Info, **kwargs
    ) -> QuerySet[Customer]:
        return queryset.filter(...)

(Edited for NDA reasons.)

I can make a proper test case next week if needed.

@bellini666
Copy link
Contributor

@alimony I mean, some examples of where that CustomerType is used.

For example, is is used in a root query like this?

@strawberry.type
class Query:
    customers: list[CustomerType] = strawberry_django.field()

Or in a nested django type:

@strawberry_type.type(SomeModel)
class SomeModelType:
    customers: list[CustomerType] = strawberry_django.field()

    # or like this
    @strawberry_django.field
    def customers(self) -> list[CustomerType]:
        ...

@alimony
Copy link
Contributor Author

alimony commented Feb 24, 2024

It's at the root, if I understand your question correctly. At the top level I have something like this:

@strawberry.type
class Query:
    customers: CustomerType | None = strawberry_django.field(extensions=[auth_required])

...

schema = strawberry.Schema(
    query=Query,
    mutation=Mutation,
    extensions=schema_extensions,
)

and then this:

@strawberry_django.type(
    Customer,
    filters=CustomerFilter,
    fields=[
        "name",
        ...
    ],
)
class CustomerType:
    name: auto
    org: OrgType
    ...

    @classmethod
    def get_queryset(cls, queryset: QuerySet[Customer], info: Info, **kwargs) -> QuerySet[Customer]:
        print('calling custom get_queryset')  # this never gets called
        return queryset.filter(...)

@bellini666
Copy link
Contributor

Hey @alimony ,

I'm trying to replicate the issue in here but it is working correctly on my tests.

Take a look at this test for example: 7b5ee33#diff-723b95803cbfa95b72fb2bc64e3000c86c7cf4920592b4dec6e15cc2ae08e821R167 . I defined it just like your example and get_queryset is being called (the test passes ok)

The only major difference I can see is that you have extensions=[auth_required] defined. Is it doing something to the field that could cause its behaviour to change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants