I have the following models example:
class TestSet(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Class(models.Model):
name = models.CharField(max_length=50)
test_set_id = models.ForeignKey(TestSet)
def __str__(self):
return self.name
class Test(models.Model):
title = models.CharField(max_length=50)
test_set_id = models.ForeignKey(TestSet)
def __str__(self):
return self.title
class ClassTest(models.Model):
class_id = models.ForeignKey(Class)
test_id = models.ForeignKey(TestSet)
memo = models.CharField(max_length=250)
def __str__(self):
return self.memo
What I want to do is when a new Class or Test is created, it should also create automatically a ClassTest entry if their test_set_id matches with the currenct entries on the database.
Example:
So there are two answers that comes in mind:
instance
is basically the just created class and you can simply operate standard django query with that do get and create additional objects.@receiver(post_save, sender=Class)
def create_test_sets(sender, instance, created, **kwargs):
if created:
///do things///
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.