Reputation: 449
Modelname.objects.filter(student__in=malerep).order_by('-Votes','-student__Percentage')
Here i want to specifically get the student who has the maximum number of votes.
What i am trying to do is, to get the list of students who stood for an election and got some votes and then sort them based on the number of votes they got. Now if i can access the first object in this list , i can make necessary modifications for the student in the database who won the election and also get the details of the rest.
I am stuck at this point. Please help
Thanx in advance.
Upvotes: 0
Views: 137
Reputation: 449
'objs=Modelname.objects.filter(student__in=malerep).order_by('-Votes','-student__Percentage')'
then using objs[0]
i was able to access the object of the student with max votes and max percentage in case of a tie.
Upvotes: 0
Reputation: 1506
At this point, you have two choice:
First, use "max" to get the max value of certain field and use that max value to retrieve the particular student instance. Good side, this is easy to implement; bad side, you need to hit database twice to achieve your goal.
Second, use raw(). You can perform all the complicate query you want. And that will solve your problem. Good side, you have the maximal freedom to preform any query; bad side, require sql knowledges.
Upvotes: 1