Sorry for a newbie question. I have a following model:
class WeightSlip(models.Model):
grossdate = models.DateTimeField(auto_now=False, auto_now_add=False)
You can create custom filter for slipdate, netweight
that will evaluate and filter this fields in db. For this you can use conditional expressions and F expression
from django.db.models import F, Case, When
class WeightSlipFilter(FilterSet):
slipdate = DateTimeFilter(method='filter_slipdate')
netweight = NumberFilter(method='filter_netweight')
class Meta:
model = WeightSlip
fields = ('slipdate', 'netweight', 'vehicle')
def filter_netweight(self, queryset, value):
if value:
queryset = queryset.annotate(netweight=F('grossweight') - F('tareweight')).filter(netweight=value)
return queryset
def filter_slipdate(self, queryset, value):
if value:
queryset = queryset.annotate(slipdate=Case(When(grossdate__gt=F('taredate'), then=F('grossdate')), default=F('taredate')).filter(slipdate=value)
return queryset