在创建另一个模型的实例时自动填充Django模型字段

时间:2018-05-01 11:39:23

标签: python django database-design

我有三个模型Student,Question和StudentAndQuestion

class Student(models.Model):
    class Meta:
        verbose_name_plural = "Students"
    name = models.CharField(max_length=150)
    surname = models.CharField(max_length=150)
    code = models.CharField(max_length=10)
#    group = models.ForeignKey(Group,on_delete=models.CASCADE)
    points = models.IntegerField(default=0)


class Question(models.Model):
    class Meta:
        verbose_name_plural = "Questions"

    text = models.CharField(max_length=1500)
    variants = models.CharField(max_length=1500)
    theme = models.OneToOneField(Theme, on_delete=models.CASCADE)
    anwser = models.CharField(max_length=1500)


class StudentAndQuestion(models.Model):
    question = models.OneToOneField(Question,on_delete=models.CASCADE)
    student = models.OneToOneField(Student,on_delete=models.CASCADE)
    is_learned = models.BooleanField(default=0)
    points = models.IntegerField(default=0)

我希望当我创建新的学生实例时Django填写StudentAndQuestion(例如为所有问题创建行并将点设置为零),当我创建问题的新实例时,将其添加到所有老学生

1 个答案:

答案 0 :(得分:0)

您可以收听保存这些模型时发出的Post Save Signal,并在处理程序中执行所需的创建。此信号使用名为created的参数调用处理程序,如果创建了新记录,则为True

请参阅the documentation on signals以了解如何注册处理程序。 样本如下

from django.db.models.signals import post_delete, post_save

from .models import Student, Question, StudentAndQuestion


def add_question_to_students(sender, instance, created, *args, **kwargs):    
question = instance
if created:
    StudentAndQuestion.objects.bulk_create([
        StudentAndQuestion(question=question, student_id=student_id)
        for student_id in Student.objects.values_list("id", flat=True)
    ])

def add_student_to_question(sender, instance, created, *args, **kwargs):    
student = instance
if created:
    StudentAndQuestion.objects.bulk_create([
        StudentAndQuestion(question=question_id, student=student)
        for question_id in Question.objects.values_list("id", flat=True)
    ])

post_save.connect(add_question_to_students, sender=Question)
post_save.connect(add_student_to_question, sender=Student)

或者,您可以覆盖两个模型中的保存以执行相同的

class Question(models.Model):
    ....
    def save(*args, **kwargs):
        is_create = self.pk is None
        instance = super().save(*args, **kwargs)
        add_question_to_students(Question, self, is_created)

class Student(models.Model):
    ....
    def save(*args, **kwargs):
        is_create = self.pk is None
        instance = super().save(*args, **kwargs)
        add_student_to_question(Student, self, is_created)

我觉得后者更清楚,因为更容易找到保存时发生的一切,但如果模型位于不同的应用程序中,它可能导致循环依赖。