Django模型输入数据

时间:2018-08-10 09:12:54

标签: django

我在此模型中相当迷失,我想在CourseScore中输入数据。课程分数将指向一名学生和该学生注册的一门课程。 我想在输入数据时进行自动计算。

    from django.db import models
    from student.models import Student
    # Create your models here.
    class Course(models.Model):
        name = models.CharField(max_length=200)
        finternalmark=models.IntegerField(default=40)
        fexternalmark = models.IntegerField(default=100)
        fullmark = models.IntegerField()

        def CalculateFullMark(self):
            self.fullmark = self.finternalmark + self.fexternalmark


        def __str__(self):
            return f'{self.name}-{self.fintegermark}-{self.fexternalmark}'

    class CourseRegistration(models.Model):
        student = models.OneToOneField(Student, on_delete=models.CASCADE)
        courses = models.ManyToManyField(Course)
        def __str__(self):
            return f'{self.student}'

    class CourseScore(models.Model):
        #entering marks for one course
        CourseRegn = models.OneToOneField(CourseRegistration, on_delete=models.CASCADE)
        internalmark = models.IntegerField()
        externalmark = models.IntegerField()
        marks = models.IntegerField()

        def CalculateMarks(self):
            self.marks = self.internalmark + self.externalmark



    class SemesterResult(models.Model):

        student = models.OneToOneField(Student, on_delete=models.CASCADE)
        courses=  models.ForeignKey(CourseScore,on_delete=models.CASCADE) # course in which the student is registered and marks are entered
        totalmarks=models.IntegerField()
        grandtotal = models.IntegerField()

        def CalculateTotalMarks(self):
            pass
            #calculate totalmarks = sum of marks scored in courses that the students opted

        def CalculateGrandTotal(self):
            pass
            #calculate grandtotal = sum of all fullmarks of the course that the student opted

1 个答案:

答案 0 :(得分:0)

我建议您使用@property装饰器设置要自动计算属性方法的属性,而不是在模型函数中进行计算:

    @property
    def marks(self):
        return self.internalmark + self.externalmark