wow i didnt see this please sir give me a clue how i can do this
You’ve got a couple different ways to do this.
-
Annotate each instance with the calculated scores and sort by that annotated value. (See Aggregation | Django documentation | Django)
-
Process the queryset as a list in your code, add the calculated score as a new attribute of the instance, and then sort the list by that value.
Technically, the first is going to be better performing because all the work will be done in the database. However, unless you’re talking about retrieving thousands of rows, you’re not going to notice a difference between the two.
The second is likely to be easier since you will just be writing Python code to do this.
sir that means i still need to bring this method get_total_scores to the views.py
def student_view_result_term(request, id):
term = get_object_or_404(Term,id=id)
student = get_object_or_404(Students, admin=request.user.id)
results = StudentResult.objects.filter(term=term, student=student)
number_of_subjects = Subjects.objects.filter(term_id=True).count()
student_result_count = Students.objects.annotate(num_student=Count('studentresult'))
student_result_count[0].num_student
print(student_result_count)
```
this is what ive done and it is showing this when i try to print it out
'''
<QuerySet [<Students: Students object (1)>, <Students: Students object (2)>]>
'''
so sir please what can i do with this
Before starting this project, did you work your way through either the Official Django Tutorial or the Django Girls Tutorial?
If you haven’t, then you should go back now and work your way through them. If you have, then you want to review part 2.
You need to be familiar with querysets - understanding what they are, and how you use them.
Additional reading is also available at Retrieving Objects and QuerySet API reference | Django documentation | Django
ok thanks so much i appreciate
and please sir i will appreciate if you give me a book to read in order to know djaango very well
I don’t have any specific resources to suggest or recommend - you might find some suitable options at GitHub - wsvincent/awesome-django: A curated list of awesome things related to Django
thanks sir but u can help me fix this while i gi through this materials one at a time
What you need to learn to get past your current level of understanding is covered by either of those two tutorials. There’s really not much I can do here that is better than what is provided there.
And you need to work your way through one of them, not just read them or copy/paste the code snippets. (There’s something about the process of physically typing in the code that enhances the learning process and improves comprehension and retention.)