Pramod
Pramod

Reputation: 652

want difference from standard value for each value of row using Django ORM

This is my Django Model

class MarkSheet(models.Model):
    marks=models.IntegerField(null=True,blank=True)
    ....

Using Django ORM I want to create extra field in queryset which contains the absolute of difference between Marks of all student and Mark given by user. For example user given mark is

UserMark=76

and our row contains

Marks
67
98
65

then I am looking some operation in querysets which gives me some operation like ABS(Marks-UserMarks)

Marksheet.objects.filter(class="12").annotate(marks_diff=abs(F('Marks')-UserMarks))

Output should be like

{"mark":67,"mark_diff":9}

Upvotes: 1

Views: 43

Answers (1)

catavaran
catavaran

Reputation: 45555

Most of SQL servers support ABS() function so you can use the extra() method:

Marksheet.objects.filter(class="12") \
                 .extra({'marks_diff': 'ABS(marks-%d)' % UserMarks})

Upvotes: 2

Related Questions