strawberry-django
strawberry-django copied to clipboard
Lookup fields not working
Thank you for this library. Really helpful, migrating from graphene-django.
i'm not sure how the lookups are supposed to work.
@strawberry.django.filters.filter(models.Submission, lookups=True)
class SubmissionFilter:
updated_date: date
updated_date__lte: date
updated_date__gte: date
It creates the types, but doesn't actually filter by the lookups. The plain singe date filter works as expected.
They are all date
types
@strawberry.django.filters.filter(models.Submission, lookups=True)
class SubmissionFilter:
updated_date: strawberry.django.auto
submissions (filters: {updatedDate: {lte: "2020-02-02"}}) {
submissionId
updatedDate
}
See https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/strawberry_django/filters.py
That is Sweet! Thank you for the quick response
I was stepping through the code and realized it was being swallowed in the
conditional. Since
updated_date__lte
didn't exist on the model. I added a re.sub
on it to strip the __[a-z]*$
from it, and then it worked in the format i was doing. Do you think that format would be good support as well, so that you can filter, but restrict the lookups to specific types per field.
The current solution hands all the lookups to the user, which may not be what every case warrants, but is really clean IMO.
I just tried out using auto
as a type, but getting
TypeError: SubmissionFilter fields cannot be resolved. Unexpected type '<class 'strawberry_django.fields.types.auto'>'
I updated to the latest version but still same error.
strawberry-graphql-django = {git = "https://github.com/strawberry-graphql/strawberry-graphql-django", rev="v0.3rc1"}
After some more playing around, I came up with the following,
- auto still not working for same reason.
- Specifying type
date
does not work as the lookup type gets changed todate
in the final schema - Manually adding
FilterLookup[date]
works as expected.
Thanks for your feedback. I agree with you that it would be nice to have possibility to define which lookups to include to your filter. I'm not sure yet how would that API look like. Feel free to suggest if you have any ideas :)
Adding support for auto
type is good idea!
We have a test case for auto
type in filters. See https://github.com/strawberry-graphql/strawberry-graphql-django/blob/main/tests/filters/test_filters.py#L12-L15
Could you share the code which is failing?
See above code snippets
Hey @aareman ,
It's been a while since you reported this issue, and since then strawberry-django has gone through a major refactor.
This issue should be fixed and working correctly in the current version, so I'm going to close this. If it is not, please tell me so I can reopen the issue