I have written a django query like this.
stud_record = Student.objects.filter()
Thus stud_record is a queryset.
Now what I want to do is, i want to calculate the total num of stud_record, and then divide it by 5.
For ex: I have 20 records in stud_record. After dividing by 5 I got 4 each. So now i want to rank the first four students as ran 1, then from 5-8 students as rank 2….16-20 students as rank 5. This rank will be inserted to the same stud_record and will be send to the template page.
How can we do it.
Currently I am getting the structure like this
[{'name': u'mark', 'roll': 71},
{'name': u'robin', 'roll': 42},
{'name': u'julien', 'roll': 39}]
After division and inserting the rank it should look like
[[{'name': u'mark', 'roll': 71, 'rank': 1},
{'name': u'robin', 'roll': 42, 'rank': 1},]
[{'name': u'julien', 'roll': 39, 'rank': 2}]]
Right now I am getting 185 results in my stud_record. After dividing 185/5 i am getting 37 as result. Now i want to give rank 1 to 1-37 students, then rank 2 to 38-75 students… like this
Add ranks to your dicts first and then group them using
itertools.groupby:out:
NB! This solution needs the list to be sorted, if its not, add:
lst.sort(key=lambda x: x['roll'],reverse=True)