简体   繁体   中英

How to create new columns from existing columns in django sqlite models.py files?

I am creating a simple result processing system using django. I made this class:

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()

Now I want to make a new column that stores the total credit value of a course ie :

Credit_Total = Credit_Lecture + Credit_Tutorial + Credit_Practical

How should I do so? I found the following possibilities but failed:

Total_Credit =Credit_Lec + Credit_Tut + Credit_Pr

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

You also could create a Property in the model like

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

add it in the Courses class

Wherever you want this info displayed, you rely to it as.

tc = Course.total_credit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM