strawberry-django icon indicating copy to clipboard operation
strawberry-django copied to clipboard

Filtering by FilterSet should honor declared django_filters filter

Open oleo65 opened this issue 1 year ago • 3 comments

Context

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.

Expected behaviour

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.

Minimal example

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. 😃

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

oleo65 avatar Aug 13 '22 19:08 oleo65

Any word on this issue?

jacobjove avatar May 23 '23 15:05 jacobjove

Hey @iacobfred and @oleo65

Wow, I did miss that issue when it got opened. I'm not really sure what might be happening, specially because after moving from graphene I stopped using django_filters and relied on filter lookups provided by this lib.

I'll soon take a look and try to replicate the issue to try to discover what might be going on.

obs. does that only happens when using this lib together with Strawberry-django-plus? Or does it happen with this alone?

bellini666 avatar May 23 '23 15:05 bellini666

I'm struggling to remember the exact circumstances for this issue but believe that it was including django_strawberry_plus.

Thanks for looking into it.

oleo65 avatar May 23 '23 15:05 oleo65