class Tbookinfo(models.Model): """教材信息""" bnumber = models.IntegerField(verbose_name='教材编号',max_length=32,primary_key=True) bname = models.CharField(verbose_name='书名',max_length=32) price = models.CharField(verbose_name='价格', max_length=32) quantity = models.CharField(verbose_name='数量', max_length=32) class Meta: db_table = 'tbookinfo'class Lmanagement(models.Model): """入库管理""" bnumber = models.IntegerField(verbose_name='教材编号',max_length=32,default='') bname = models.CharField(verbose_name='书名',max_length=32,default='') quantity = models.CharField(verbose_name='数量', max_length=32,default='') loquantity = models.CharField(verbose_name='入库数量', max_length=32,default='') warehousingtime = models.DateTimeField(verbose_name='入库时间', max_length=32,auto_now_add=True) class Meta: db_table = 'lmanagement' class Omanagement(models.Model): """出库管理""" bnumber = models.IntegerField(verbose_name='教材编号',max_length=32,default='') bname = models.CharField(verbose_name='书名',max_length=32,default='') quantity = models.CharField(verbose_name='数量', max_length=32,default='') loquantity = models.CharField(verbose_name='出库数量', max_length=32,default='') outboundtime = models.DateTimeField(verbose_name='出库时间', max_length=32,auto_now_add=True) class Meta: db_table = 'omanagement'当入库数量或出库数量改变时,Tbookinfo的quantity 也要跟着改变,怎么写出代码
时间: 2023-06-07 11:10:51 浏览: 108
可以在Lmanagement和Omanagement的save()方法中,通过更新Tbookinfo的quantity来实现。
代码如下:
class Tbookinfo(models.Model):
"""教材信息"""
bnumber = models.IntegerField(verbose_name='教材编号',max_length=32,primary_key=True)
bname = models.CharField(verbose_name='书名',max_length=32)
price = models.CharField(verbose_name='价格', max_length=32)
quantity = models.CharField(verbose_name='数量', max_length=32)
class Meta:
db_table = 'tbookinfo'
class Lmanagement(models.Model):
"""入库管理"""
bnumber = models.IntegerField(verbose_name='教材编号',max_length=32,default='')
bname = models.CharField(verbose_name='书名',max_length=32,default='')
quantity = models.CharField(verbose_name='数量', max_length=32,default='')
loquantity = models.CharField(verbose_name='入库数量', max_length=32,default='')
warehousingtime = models.DateTimeField(verbose_name='入库时间', max_length=32,auto_now_add=True)
class Meta:
db_table = 'lmanagement'
def save(self, *args, **kwargs):
tbookinfo = Tbookinfo.objects.get(bnumber=self.bnumber)
tbookinfo.quantity = str(int(tbookinfo.quantity) + int(self.loquantity))
tbookinfo.save()
super(Lmanagement, self).save(*args, **kwargs)
class Omanagement(models.Model):
"""出库管理"""
bnumber = models.IntegerField(verbose_name='教材编号',max_length=32,default='')
bname = models.CharField(verbose_name='书名',max_length=32,default='')
quantity = models.CharField(verbose_name='数量', max_length=32,default='')
loquantity = models.CharField(verbose_name='出库数量', max_length=32,default='')
outboundtime = models.DateTimeField(verbose_name='出库时间', max_length=32,auto_now_add=True)
class Meta:
db_table = 'omanagement'
def save(self, *args, **kwargs):
tbookinfo = Tbookinfo.objects.get(bnumber=self.bnumber)
tbookinfo.quantity = str(int(tbookinfo.quantity) - int(self.loquantity))
tbookinfo.save()
super(Omanagement, self).save(*args, **kwargs)
阅读全文