[Django]-How make conditional expression on Django with default value of object?

10👍

F is used to perform this. So, your code should look like:

   from django.db.models import Case, F, Value, When, DecimalField
   o = Object.objects.annotate(custom_price=Case(
                When(price=1, then=Value(2)),
                default=F('price'),
                output_field=DecimalField(),
            )
        )

Leave a comment