doctorsong

联表小案例

from django.db import models

class TableA(models.Model):
    experiment_number = models.IntegerField()
    specimen_count = models.IntegerField()

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        # 在保存TableA对象后生成对应数量的TableB对象
        for i in range(1, self.specimen_count + 1):
            TableB.objects.create(table_a=self, sample_number=i)
    
class TableB(models.Model):
    table_a = models.ForeignKey(TableA, on_delete=models.CASCADE)
    sample_number = models.IntegerField()

 

posted on 2023-08-09 17:24  songyu666  阅读(4)  评论(0编辑  收藏  举报

导航