-
-
Save melizeche/de8add80ebe5a1689a77ea0ad46e4f83 to your computer and use it in GitHub Desktop.
How to use django-filter to add a DRF filter using dates and slugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleFilter(filters.FilterSet): | |
start_date = django_filters.DateFilter(name="date", lookup_type='gte') | |
end_date = django_filters.DateFilter(name="date", lookup_type='lte') | |
# How to filter by a foreign key that uses slug as a lookup | |
foo = django_filters.ModelMultipleChoiceFilter( | |
queryset=MyModel.objects.all(), | |
to_field_name='slug', | |
conjoined=True, | |
) | |
class Meta: | |
model = TradingSystemDataPoint | |
fields = ['date', 'start_date', 'end_date', 'foo', ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment