Skip to content

Instantly share code, notes, and snippets.

@dkarchmer
Created May 2, 2016 00:26
Show Gist options
  • Save dkarchmer/fa19681683073a54be456a84e3458c37 to your computer and use it in GitHub Desktop.
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
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