How to pass models method value to Views

First, an issue of terminology. You wrote:

Correct. This is the wrong way to look at it. You don’t “pass the model method in to the views.” You access the model method on the instance of the object, from the view.

Since you’re no longer using the @property decorator in the model, then you need to call the function as a function, and your reference would be a = results.get_total_scores().

i tried it before and it showed " required one argument self"

Using what code in the view?

Again, just saying that something didn’t work provides us with no information.

from this same code sir it shows this
“”"

AttributeError at /student_view_result_term/1/

‘QuerySet’ object has no attribute ‘get_total_scores’
“”"

You either did not try what I posted, or you’ve made some other change.

In all cases, with every post, on every attempt - you need to post the actual code that you’re using with that attempt. We cannot diagnose a problem from just the error message. We do not know what change you made or what other changes you may have made to the code since a previous post.

In the general case, with this error, you’re trying to call that function on a queryset and not on an individual instance of the model. How you are using it in the view would indicate the source of the issue.

sir i didn’t make much changes i just changed the names of the fields for naming convention i didn’t change any other thing

This statement in your view:

Is it intended to return one StudentResult or multiple StudentResult?

It is suppose to return one student result

In that case, it should be a get and not a filter. A filter returns a list - you would need to process each element of that list individually.

if i use get it shows this
‘’’

MultipleObjectsReturned at /student_view_result_term/1/

get() returned more than one StudentResult – it returned 2!
‘’’

Then you either have something wrong with your data or with how you’re modelling your data.

should i show my model?

You did in an earlier post.

There’s nothing wrong with the model itself.

However, it might not appropriately match the data you’re trying to use with it with. (That’s what I mean by “modelling your data”.)

You either have some invalid data in your model, or you didn’t design your model properly for the data you are working with.

Those aren’t, strictly speaking, Django issues. That’s a design and specification issue.

i found an alternative where i have to do the calculations in template. but i dont think its a good practice thou it is workig

{% for result in results %}

                                <tr>

                                    <td>{{ result.id }}</td>

                                    <td>{{ result.student.admin.first_name }}</td>

                                    <td>{{ result.subject.subject_name }}</td>

                                    <td>{{ result.assignment_marks }}</td>

                                    <td>{{ result.test_marks }}</td>

                                    <td>{{ result.exam_marks }}</td>

                                    <td><b>{{ result.get_total_scores }}</b></td>

                                     

                                     {% comment %} <td><b>{{ subject }}</b></td>  {% endcomment %}

                                    <td>{{ result.get_total_scores |div:subject|floatformat }}</td>

                                    <td>

                                        {% if result.exam_marks >= 40 %}

                                            <span class="alert alert-success" >Pass</span>

                                        {% else %}

                                            <span class="alert alert-danger" >Fail</span>

                                        {% endif %}

                                    </td>

                                </tr>

                                {% endfor %}

                            </table>

                            {% else %}

                            <h1>No result yet for this term...</h1>

                            {% endif %}

So by using this, you are saying that results will return multiple values.

no Sir it worked fine with this but i dont thnk it is a good practice

You’re missing my point here.

You wrote:

However, in your template you have:

This means that your template is set up to handle multiple results. It’s designed to render a queryset and not an individual entry.

(This issue has nothing to do with your calculations - this is a separate point addressing your understanding of the model and the data you are working with.)

Now, regarding your calculation, you could create another method on the model to perform the complete calculation, and reference that in your template.

sorry ive been trying to have a responsive naming it should be something like this
“{for my_result in result}”
but it is giving the expected value… that what i mean

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