我在这个模型中很迷茫,我想在课程分数中输入数据。课程分数将指向一名学生和一名学生注册的课程。 我想在数据输入时进行自动计算。
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
我建议你使用@property装饰器制作你想要自动计算属性方法的属性,而不是在你的模型函数中计算它:
@property
def marks(self):
return self.internalmark + self.externalmark