GithubHelp home page GithubHelp logo

Comments (9)

rpkilby avatar rpkilby commented on August 27, 2024

Hi @jackdbernier. If this is a one off problem, I would try something like the following:

class SomeFilter(filters.FilterSet):
    # Notice the double underscores separating `a_field` and `in`
    a_field__in = filters.InSetCharFilter(name='a_field', lookup_type='in', choices=choices)

    class Meta:
        model = SomeModel
        # I recommend explicitly declaring relevant lookups, instead of using AllLookupsFilter.
        fields = {
            'a_field': ['exact', 'gt', 'gte', 'lt', 'lte', 'etc...'],
        }

If this is a common issue for you, you could also override the behavior of fix_filter_field by subclassing FilterSet.

from django-rest-framework-filters.

rpkilby avatar rpkilby commented on August 27, 2024

btw - I'm currently working on a patch for django-filter that should negate the need for a fix in drf-filters (fix_filter_field would no longer be necessary).

from django-rest-framework-filters.

rpkilby avatar rpkilby commented on August 27, 2024

Hi @jackdbernier, django-filter v0.13.0 now supports in & range csv lookups, as well as date transforms. The upcoming release of drf-filters (v0.8.0) should be released soon, and no longer relies on fix_filter_field.

from django-rest-framework-filters.

 avatar commented on August 27, 2024

Great I'll give a try at the new version. Thanks.

from django-rest-framework-filters.

evenicoulddoit avatar evenicoulddoit commented on August 27, 2024

As a follow up to this here's one way of solving the problem:

import rest_framework_filters as filters

from models import Foo


class FooFilter(filters.FilterSet):
    class Meta:
        model = Foo
        fields = {
            'bar': ['exact', 'in'],
        }

Then you can use bar__in=baz,zulu as well as bar__in!=baz,zulu as expected.

I did need to use the fields dictionary though to explicitly provide the lookup types. Perhaps that means some follow-up work would just solve this if the "default all" approach used by django-rest-framework-filters included in?

from django-rest-framework-filters.

rpkilby avatar rpkilby commented on August 27, 2024

@evenicoulddoit - AllLookupsFilter should include in filtering. What version of drf-filters are you using?

from django-rest-framework-filters.

evenicoulddoit avatar evenicoulddoit commented on August 27, 2024

@rpkilby djangorestframework-filters==0.8.0

from django-rest-framework-filters.

sean-bennett112 avatar sean-bennett112 commented on August 27, 2024

Not sure if this should be in the same issue or a separate one, but I can confirm just running into this as well. Using 0.8.0, it doesn't appear that in is included in AllLookupsFilter.

Rather, when I send up ?id__in=2,3,4,5,6, for example, I weirdly only get returned the entity with id 6.

I'm running into this in my test suite when using

self.client.get(self.api_url, data={'id__in': [2, 3, 4, 5, 6]})
class DeviceFilter(FilterSet):
    organization = RelatedFilter(organization_filter, name='organization')
    project = RelatedFilter(project_filter, name='project')

    id = AllLookupsFilter(name='id')
    created = AllLookupsFilter(name='created')
    updated = AllLookupsFilter(name='updated')
    name = AllLookupsFilter(name='name')

    class Meta:
        model = Device

from django-rest-framework-filters.

sean-bennett112 avatar sean-bennett112 commented on August 27, 2024

Actually ignore that, this was an error on my part. Passing the list in as a string 2,3,4,5,6 fixes everything for me.

from django-rest-framework-filters.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.