Home > Blockchain >  Using When and Less then in Django DRF?
Using When and Less then in Django DRF?

Time:10-21

I am trying to check where a number is GreaterThan another number in the request.data and set the value if condition is true.

ExampleModel.objects.filter(
                        tic=request.data['tic']).update(last_high=When(GreaterThan(F('last_high'), request.data['high']), then=0))

Error:

django.db.utils.OperationalError: near "WHEN": syntax error

I am not sure how to proceed from here, trying to understand the documentation but I can't seem to find why it won't work.

Documentation

CodePudding user response:

Could something like that do the trick ?

ExampleModel.objects.filter(tic=request.data['tic'], last_high__gt=request.data['high']).update(last_high=0)

EDIT: After some back and forth in the comments section, the right answer would be

ExampleModel.objects.filter(tic=request.data['tic']).update(last_high=Case(When(LessThan(F('high_price'), request.data['high']), then=Value(101)), default=Value(0)))
  • Related