Created
May 2, 2016 00:26
-
-
Save dkarchmer/fa19681683073a54be456a84e3458c37 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