Skip to content

Instantly share code, notes, and snippets.

@jpadilla
Created September 3, 2015 14:47
Show Gist options
  • Save jpadilla/7d2b7edfc4397160795d to your computer and use it in GitHub Desktop.
Save jpadilla/7d2b7edfc4397160795d to your computer and use it in GitHub Desktop.

Custom lookup types

By default django-filter uses Django's ORM built-in field lookups. If you want to globally accept specific lookups you can do the following:

from django_filters import filters


filters.LOOKUP_TYPES = ['gt', 'gte', 'lt', 'lte', 'custom_lookup_type']

Choices help text

If you want the ChoiceField created from LOOKUP_TYPES to have human-friendly options you can do the following:

from django_filters import filters


filters.LOOKUP_TYPES = [
    ('', '---------'),
    ('exact', 'Is equal to'),
    ('not_exact', 'Is not equal to'),
    ('lt', 'Lesser than'),
    ('gt', 'Greater than'),
    ('gte', 'Greater than or equal to'),
    ('lte', 'Lesser than or equal to'),
    ('startswith', 'Starts with'),
    ('endswith', 'Ends with'),
    ('contains', 'Contains'),
    ('not_contains', 'Does not contain'),
]
@carltongibson
Copy link

OK. Good start. I'll have a think over the weekend.

(Thanks for the work on this!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment