You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from django.db.models.signals import post_save
from django.dispatch import receiver
from basic_info.models import WarehouseModel
from goods_info.models import GoodsModel, GoodsInventoryModel
import logging
logger = logging.getLogger('my')
@receiver(post_save, sender=WarehouseModel)
def create_goods_inventory(sender, instance, created, **kwargs):
"""
创建信号监控函数
创建新仓库之后, 给所有的货品在当前的新仓库 新增库存数据
"""
if created:
logger.info('创建当前的新仓库——新增库存数据')
if isinstance(instance, WarehouseModel):
# 首先查询所有的货品id
ids = GoodsModel.objects.values_list('id', flat=True).all()
inventory_list = []
for gid in ids:
inventory_list.append(GoodsInventoryModel(
goods_id=gid,
warehouse_name=instance.name,
warehouse=instance
))
# 一定要采用批量新增函数bulk_create
GoodsInventoryModel.objects.bulk_create(inventory_list)
else:
logger.info('不是WarehouseModel类型所以不需要创建库存数据')