How to pass models method value to Views

please i have been trying to return a value from a method in my models to my views but it hasn’t been working. can somebody help me out? this is what i have

    id = models.AutoField(primary_key=True)
    student_term = models.ForeignKey(Term, on_delete=models.CASCADE)
    student_id = models.ForeignKey(Students, on_delete=models.CASCADE)
    subject_id = models.ForeignKey(Subjects, on_delete=models.CASCADE)
    subject_exam_marks = models.FloatField(default=0)
    subject_test_marks = models.FloatField(default=0)
    subject_assignment_marks = models.FloatField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    objects = models.Manager()
    
    @property
    def get_total_scores(self):
        return self.subject_exam_marks +self.subject_test_marks self.subject_assignment_marks```

i want to use the value returned by get_total_scores in my views. how do i pass it to the views

You access it as a property of the object being referenced in your view - just like any other attribute of an object.

For example, if you have an instance of MyModel named my_model, the reference to the property in the view would be my_model.get_total_scores.

thanks… i tried it but it didn’t bring out the values
‘’’
def student_view_result_term(request, id):

student_term = get_object_or_404(Term,id=id)

student_id = get_object_or_404(Students, admin=request.user.id)



result = StudentResult.objects.filter(student_term=student_term, student_id=student_id)



subject_id = Subjects.objects.filter(term_id=True).count()



a = StudentResult.get_total_scores

print(a)

‘’’
i want to get the value gotten from the score then divide it with the number of subject in a term which is filtered in the variable “subject_id” please how do i get the value
‘’’
<property object at 0x000001743D74D130>
‘’’
it shows this when i want to print it out

Side note - you should enclose your code between lines of three backticks -`, not single quotes. If you change your quotes, it’ll be formatted as you posted it.

Now, what you’re seeing is caused by:

StudentResult is a reference to the model and not a reference to an instance of the model.
If you want the total scores from result, the reference would be to result.get_total_scores.

sorry about that am just new here

No worries - we’re here to help.

1 Like
def student_view_result_term(request, id):
    student_term = get_object_or_404(Term,id=id)
    student_id = get_object_or_404(Students, admin=request.user.id) 
    
    result = StudentResult.objects.filter(student_term=student_term, student_id=student_id)
    
    subject_id = Subjects.objects.filter(term_id=True).count()
    
    a = StudentResult.get_total_scores
    print(a)

ok here is the code

please sir help me out … tell me what to do

I did. My previous response answered your question.

i tried it too but it didn’t work

Please post the corrected code that you tried and what you got as a result.

Also, it’s likely going to be helpful if you describe exactly what it is that you want “get_total_scores” to provide as a value.

(Note for future reference - you’ll always want to do this in the future for this, or any other issues you wish to discuss here. Saying something “doesn’t work” doesn’t help us help you. In all cases you should post the code you’ve tried and what happens as a result of that attempt.)

please forgive my manners

Again, no problems there and no need to apologize. You haven’t done anything wrong - I’m just trying to help you to best put us in a position where we can help you with your issue.

1 Like

i am trying to get the average score of a particular student, i’ve gotten the total mark from the accessments which is from this method

class StudentResult(models.Model):
    id = models.AutoField(primary_key=True)
    student_term = models.ForeignKey(Term, on_delete=models.CASCADE)
    student_id = models.ForeignKey(Students, on_delete=models.CASCADE)
    subject_id = models.ForeignKey(Subjects, on_delete=models.CASCADE)
    subject_exam_marks = models.FloatField(default=0)
    subject_test_marks = models.FloatField(default=0)
    subject_assignment_marks = models.FloatField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    objects = models.Manager()
    
    
    def get_total_scores(self):
        return self.subject_exam_marks +self.subject_test_marks + self.subject_assignment_marks

and this have been passed in directly to the template and its working fine. so i want to use this method to divide the number of subject offered in a particular term
example of what i want to do""“def get_total_scores / number of subject in term”""
and this is my views

def student_view_result_term(request, id):
    student_term = get_object_or_404(Term,id=id)
    student_id = get_object_or_404(Students, admin=request.user.id) 
    
    results = StudentResult.objects.filter(student_term=student_term, student_id=student_id)
    subject_id = Subjects.objects.filter(term_id=True).count()
    

    
    context = {
        'student_term':student_term,
        'results':results,
        'subject_id':subject_id
        
    }
    return render(request, "student_template/student_view_result_term.html", context)

so please i just need help

But you haven’t made the change that I showed you at How to pass models method value to Views - #4 by KenWhitesell

Those types of calculations belong in the views, not in the templates. You perform your data retrievals and calculations in the view, and then supply the data to the templates to be rendered.

i removed it lemme put it back

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(student_term=term, student_id=student)



    number_of_subjects = Subjects.objects.filter(term_id=True).count()



    a = results.get_total_scores

    print(a)
    
    context = {
        'term':term,
        'results':results,
        'subject':number_of_subjects
        
        
    }
    return render(request, "student_template/student_view_result_term.html", context)

this is my views.py

class StudentResult(models.Model):

    id = models.AutoField(primary_key=True)

    term = models.ForeignKey(Term, on_delete=models.CASCADE)

    student = models.ForeignKey(Students, on_delete=models.CASCADE)

    subject = models.ForeignKey(Subjects, on_delete=models.CASCADE)

    exam_marks = models.FloatField(default=0)

    test_marks = models.FloatField(default=0)

    assignment_marks = models.FloatField(default=0)

    created_at = models.DateTimeField(auto_now_add=True)

    updated_at = models.DateTimeField(auto_now=True)

    objects = models.Manager()

       

    def get_total_scores(self):

        return self.exam_marks +self.test_marks + self.assignment_marks

here is the models am fetching the data from

You’ve changed your model, too - you no longer have the property decorator on the get_total_scores. Why? (It’s not wrong to have done so - this can be made to work either way. I just want to see what you understand about what’s going on here.)

yes this was the act of trying to fix the code… i created a student result model and in this model i got the total number of marks gotten from a student which i passed in to the method in the same model. now in views.py i wanted to get the number of subjects per term so i can divide it with the total score which is in the method by the problem is i cant pass in the model’s method into the views.py to make the calculation