[Django]-Filtering queryset if one value is greater than another value

15👍

Django cannot write a query which is conditioned on a field whose value is unknown. You need to use a F expression for this:

from django.db.models import F

queryset = Material.objects.filter(total__lte = F('min_quantity'))

And your FilterSet becomes:

po_list = MaterialFilter(request.GET, queryset = Material.objects.filter(total__lte=F('min_quantity')))

From the docs:

An F() object represents the value of a model field or annotated
column. It makes it possible to refer to model field values and
perform database operations using them without actually having to pull
them out of the database into Python memory

Leave a comment