如何在django sqlite models.py文件中从现有列创建新列?

时间:2018-04-22 19:10:55

标签: python django web sqlite

我正在使用django创建一个简单的结果处理系统。 我做了这个课:

class Course(models.Model):
    Course_Code = models.CharField(max_length=50,unique=True,primary_key = True)
    Course_Name = models.CharField(max_length=50)
    Dept = models.CharField(max_length=50)
    Credit_Lecture = models.IntegerField()
    Credit_Tutorial = models.IntegerField()
    Credit_Practical = models.IntegerField()
    Max_Marks_MidTerm = models.IntegerField()
    Max_Marks_Theory = models.IntegerField()
    Max_Marks_Pr = models.IntegerField()

现在我想创建一个新列,用于存储课程的总学分值,即:

Credit_Total = Credit_Lecture + Credit_Tutorial + Credit_Practical

我该怎么办? 我发现了以下可能性但失败了:

Total_Credit =Credit_Lec + Credit_Tut + Credit_Pr

Total_credit = SELECT CAST(Credit_lec AS VARCHAR(255)) AS credit_lec FROM Course;

1 个答案:

答案 0 :(得分:0)

您还可以在模型中创建一个属性,如

@property
def total_credit(self):
    tc = self.Credit_Lecture + self.Credit_Tutorial + self.Credit_Practical
    return tc

将其添加到Courses类

无论您希望在何处显示此信息,您都可以依赖它。

tc = Course.total_credit