比如我们有这样三个model, 一个代表作业,一个代表作业发布的班级。HomeworkClass 表有外键homework_id关联Homework。
https://docs.djangoproject.com/zh-hans/2.2/topics/db/aggregation/
from django.db import models
class Homework(models.Model):
id = models.BigIntegerField(primary_key=True)
user_id = models.BigIntegerField(blank=True, null=True) # 发布者id
published_time = models.DateTimeField(blank=True, null=True) # 作业布置时间
end_time = models.DateTimeField(blank=True, null=True) # 作业截止时间
class Meta
managed = False
db_table = 'homework'
class HomeworkClass(models.Model):
id = models.BigAutoField(primary_key=True)
homework_id = models.ForeignKey(to=Homework, on_delete=models.SET_NULL,to_field='id',
blank=True, null=True, db_constraint=False, db_column='homework_id') # 作业id
class_id = models.BigIntegerField(blank=True, null=True) # 行政班班级id
tmp_class_id = models.BigIntegerField(blank=True, null=True) # 临时班班级id
class Meta:
managed = False
db_table = 'homework_class'
class HomeworkStudent(models.Model):
id = models.BigIntegerField(primary_key=True)
# homework_id = models.BigIntegerField(blank=True, null=True) # 作业id
homework_id = models.ForeignKey