Hi everyone,
I'm working on an online database of motion picture filmstocks. We have very good data on many attributes that can be represented in a search form by checkboxes, e.g., gauge (8mm, Super 8, 16mm, 35mm) or type (Reversal, Negative, Positive, Other).
What we don't have good data on is the year a filmstock was introduced or discontinued. Month and day are highly unlikely to ever be known for this data so I'm modeling it with a PostgreSQL IntegerRangeField. When people decide to search by years I would like to remove from search consideration any stock missing both year of introduction and year discontinued. From that queryset I would like to use an overlap filter. I've been trying to go about it this way:
class StockFilter(filters.FilterSet):
class Meta:
model = Stock
form = YearsProducedForm
fields = [
'company',
'gauges',
'chromas',
'bases',
'types',
'usages',
'balances',
'years_produced',
]
filter_overrides = {
IntegerRangeField: {
'filter_class': filters.NumericRangeFilter,
'extra': lambda f: {
'lookup_expr': 'overlap'
},
}
}
class StockList(generics.ListAPIView):
serializer_class = StockSerializer
filterset_class = StockFilter
def get_queryset(self):
if self.request.query_params['years_produced_min'] or self.request.query_params['years_produced_max']:
try:
start = int(self.request.query_params['years_produced_min'])
except ValueError:
start = None
try:
end = int(self.request.query_params['years_produced_max'])
except ValueError:
end = None
return Stock.objects.exclude(years_produced__exact=(None, None))
else:
return Stock.objects.all()
"
Regular field lookups are available in addition to several containment lookups, including overlap, contains, and contained_by. More details in the Django docs.
If the lower limit value is provided, the filter automatically defaults to startswith as the lookup and endswith if only the upper limit value is provided.
"
and the startswith behavior appears to be what I am seeing.
I don't understand how I can disable this and get the overlap behavior I'm looking for and I'd be grateful for any insights or suggestions, thanks.
Eric