I am coming from graphene and have been migrating multiple projects to strawberry with the current being a fairly large api project. The current project relies heavily on django_filters
for streamlining the filtering experience.
As far as I can tell from past issue and merge requests strawberry_django
and combination with strawberry_django_plus
should support filtering models by FilterSet
classes. (This is what I found here among others: #15 #20 )
The declared filters have been working in combination with graphene and are valid django_filters which should work in strawberry context as well.
After hours of debugging I am somewhat stuck in finding the root cause of this issue. π€·
Long story short, they get properly applied to the schema definition and to the field. But they do not get applied to the queryset during resolving of the objects and therefore being ignored.
If I declare a FilterSet
class which I either apply to the strawberry.django.type
or the field directly it should honor the declared filters during resolving of the fields.
I put together a minimal example showing some custom filtering with lookup expressions valid to django querysets.
class Asset(models.Model):
id = models.AutoField(primary_key=True, unique=True)
buy_date = models.DateTimeField(db_index=True)
@strawberry.django.filters.filter(Asset)
class AssetFilter(FilterSet):
year: int = NumberFilter(field_name='buy_date', lookup_expr='year')
@strawberry.django.type(Asset)
class AssetNode():
id: strawberry.auto
buy_date: strawberry.auto
@strawberry.type
class AssetQuery:
assets_pure: list[AssetNode] = strawberry.django.field(filters=AssetFilter)
I suspect that there is some issue with instanciation of the actual FilterSet
class instance and further down applying it to the queryset. Inspecting with the debugger it seems to me that the class instance is not properly initialized and the __init__
method won't be hit.
Any help here is appreciated. If you need more info please let me know. π
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too