4👍
✅
In this case the best way would be to use Django’s queryset extra() method:
MyModel.objects.extra(where=['abs(field1 - %s) <= 50'], params=[binding_value])
👤che
3👍
If i’m not thinking the wrong way (please let me now if i am), you can “translate” you query a little to use simple django filtering.
First, it’s True that:
abs(A - B) <= 50
is equal to:
-50 <= A - B <= 50
Then, this is equal to:
-50 + B <= A <= 50 + B
Then, your query could be like this:
MyModel.objects.filter(field1__range=(-50+binding_value,50+binding_value))
0👍
from django.db.models import Func
class Abs(Func):
function = "abs"
MyModel.objects.annotate(
expr=Abs("field1") - binding_value
).filter(
expr__lte=50
)
Edit: see also https://stackoverflow.com/a/35658634/10840
- [Django]-'Cannot alter upload handlers' while trying to upload file
- [Django]-Reset primary key of Postgres database with Django
- [Django]-IF statement not working with list difference
- [Django]-How do you render a django form in a view?
Source:stackexchange.com