1
Select IF(5>10, true, false);

I want to write this using Django model.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Jatin
  • 23
  • 7
  • Could you share your model layout, and where you want to use this? Maybe take a look at https://docs.djangoproject.com/en/2.1/topics/db/queries/ – Tomas Jacobsen Nov 15 '18 at 11:34
  • Please elaborate more so that I can provide you exact solution If you you are looking for ternary operator then have a look on https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator – Anoop Kumar Nov 15 '18 at 11:37
  • With aggregate ? https://docs.djangoproject.com/en/2.1/topics/db/aggregation/ – Bast Nov 15 '18 at 11:42

1 Answers1

1

you can use case when. see example below:

YourModel.objects.annotate(
    discount=Case(
        When(field_value__lte=5, then=Value(1)),
        default=Value(0),
        output_field=IntegerField(),
    ),
)

you can write case when as you needed. see documentation in this link https://docs.djangoproject.com/en/2.1/ref/models/conditional-expressions/

vorujack
  • 1,778
  • 16
  • 22