Alotab
Alotab

Reputation: 19

'CourseTake' object has no attribute 'points'

models.py

class Course(models.Model):
title = models.CharField(max_length=255)
credit_hours = models.IntegerField()
instructor = models.ForeignKey(Instructor, on_delete=models.SET_NULL, null=True, related_name='course')
def __str__(self) -> str:
    return self.title



class CourseTake(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='coursetake') #similar to reviews in the product class
course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course')
grade = models.PositiveIntegerField()

class Meta:
    unique_together = [['student', 'course']]

class SimpleCourseSerializer(serializers.ModelSerializer): class Meta: model = Course fields = ['title','credit_hours']

class CourseTakeSerializer(serializers.ModelSerializer):
course = SimpleCourseSerializer()
points = serializers.SerializerMethodField()
grade_points = serializers.SerializerMethodField()

class Meta:
    model = CourseTake
    fields = ['id','course', 'grade', 'points', 'grade_points']

def get_points(self, coursetake: CourseTake):
    if coursetake.grade >= 90:
        return '4'
    elif coursetake.grade >= 70:
        return '3'
    elif coursetake.grade >= 50:
        return '2'
    return '1'

#TRY AND ERROR  
#Terminal: 'CourseTake' object has no attribute 'points'
def get_grade_points(self, coursetake: CourseTake):
    return coursetake.course.credit_hours * coursetake.points 

I want to calculate grade points, which will be used later to calculate each student's GPA score. So the get_grade_point()  will return the credit_hours *  points. My problem is that the points field is not part of the model or serializer. I created a function to calculate the points for each course. Because I defined the points, Django keeps saying it's not an attribute anytime I try to access the point values. Is there a way to access the points value inside the get_grade_function?

Image for better view

Upvotes: 0

Views: 47

Answers (2)

samydgr
samydgr

Reputation: 3

The value of the grade point depends on the point, and since the point is not stored in the database, it is better to calculate them both in a function. The amount of points is independent of the grade point

def et_grade_points(self, coursetake: CourseTake):
    if coursetake.grade >= 90:
        return coursetake.course.credit_hours * 4
    elif coursetake.grade >= 70:
        return coursetake.course.credit_hours * 3
    elif coursetake.grade >= 50:
        return coursetake.course.credit_hours * 2
    return coursetake.course.credit_hours 

def get_points(self, coursetake: CourseTake):
    if coursetake.grade >= 90:
        return '4'
    elif coursetake.grade >= 70:
        return '3'
    elif coursetake.grade >= 50:
        return '2'
    return '1'

Upvotes: 0

Hashem
Hashem

Reputation: 645

Depends on what you want I suggest to use Save method on model

def save(self, *args, **kwargs):

If you want to save result in database, Or you can use @proparty to make read only method also inside your model class if you don’t want to save it

After that you can use it easily in serializer

Upvotes: 0

Related Questions