Django Order By Query on Methods in Models -
i have model:
class user(models.model): uid = models.autofield(primary_key=true) email = models.emailfield(max_length=200, unique=true) password = models.charfield(max_length=200) name = models.charfield(max_length=200, default='') contact = models.charfield(max_length=10) cash_amount = models.integerfield(default=25000) share_amount = models.integerfield(default=0) def __str__(self): return self.email def total_amount(self): return self.cash_amount + self.share_amount
how write queryset ordering result on basis of total_amount(), tried below query:
user.objects.order_by('total_amount()')
but fielderror: invalid order_by arguments: ['total_amount()'] comes.
is there other way possible of writing query ordering result on basis of cash_amount + share_amount without creating total_amount().
you can try using extra
user.objects.extra( select={'field_sum' : 'cash_amount + share_amount'}, order_by=('field_sum',) )
Comments
Post a Comment