# How to pass models method value to Views

**URL:** <https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379>\
**Category:** Getting Started\
**Created:** [April 26, 2022, 5:54pm UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379 "2022-04-26T17:54:32Z")\
**Posts on this page:** 10\
**Page:** 3

<div class="post-metadata">

**Author:** ![Gentility01](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gentility01/32/7790_2.png) [@Gentility01](https://forum.djangoproject.com/u/Gentility01)\
**Post date:** [April 28, 2022, 3:24pm UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/41 "2022-04-28T15:24:18Z")

</div>

wow i didnt see this please sir give me a clue how i can do this

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 28, 2022, 5:12pm UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/42 "2022-04-28T17:12:51Z")

</div>

> [@Gentility01](#):
>
> now i want to get the average of all the students and give them position based on the score they got. example a student with the highest score should have the first position. etc am just thinking on how i can do that

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](https://docs.djangoproject.com/en/4.0/topics/db/aggregation/))

- 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.

---

<div class="post-metadata">

**Author:** ![Gentility01](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gentility01/32/7790_2.png) [@Gentility01](https://forum.djangoproject.com/u/Gentility01)\
**Post date:** [April 29, 2022, 5:34am UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/43 "2022-04-29T05:34:09Z")

</div>

sir that means i still need to bring this method get\_total\_scores to the views.py

---

<div class="post-metadata">

**Author:** ![Gentility01](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gentility01/32/7790_2.png) [@Gentility01](https://forum.djangoproject.com/u/Gentility01)\
**Post date:** [April 29, 2022, 6:05am UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/44 "2022-04-29T06:05:55Z")

</div>

````auto
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
````

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 29, 2022, 10:38am UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/45 "2022-04-29T10:38:59Z")

</div>

Before starting this project, did you work your way through either the [Official Django Tutorial](https://docs.djangoproject.com/en/4.0/intro/tutorial01/) or the [Django Girls Tutorial](https://tutorial.djangogirls.org/en/)?

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](https://docs.djangoproject.com/en/4.0/topics/db/queries/#retrieving-objects-1) and [QuerySet API reference | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/models/querysets/#queryset-api-reference)

---

<div class="post-metadata">

**Author:** ![Gentility01](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gentility01/32/7790_2.png) [@Gentility01](https://forum.djangoproject.com/u/Gentility01)\
**Post date:** [April 29, 2022, 11:06am UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/46 "2022-04-29T11:06:17Z")

</div>

ok thanks so much i appreciate

---

<div class="post-metadata">

**Author:** ![Gentility01](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gentility01/32/7790_2.png) [@Gentility01](https://forum.djangoproject.com/u/Gentility01)\
**Post date:** [April 29, 2022, 12:06pm UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/47 "2022-04-29T12:06:58Z")

</div>

and please sir i will appreciate if you give me a book to read in order to know djaango very well

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 29, 2022, 12:31pm UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/48 "2022-04-29T12:31:51Z")

</div>

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](https://github.com/wsvincent/awesome-django)

---

<div class="post-metadata">

**Author:** ![Gentility01](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gentility01/32/7790_2.png) [@Gentility01](https://forum.djangoproject.com/u/Gentility01)\
**Post date:** [April 30, 2022, 10:10am UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/49 "2022-04-30T10:10:10Z")

</div>

thanks sir but u can help me fix this while i gi through this materials one at a time

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 30, 2022, 10:29am UTC](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379/50 "2022-04-30T10:29:59Z")

</div>

> [@KenWhitesell](#):
>
> Before starting this project, did you work your way through either the [Official Django Tutorial](https://docs.djangoproject.com/en/4.0/intro/tutorial01/) or the [Django Girls Tutorial](https://tutorial.djangogirls.org/en/)?

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.)

[Previous page](https://forum.djangoproject.com/t/how-to-pass-models-method-value-to-views/13379.md?page=2)
