Python 通过django检查MyModel.m2m_字段中的更新方法
我想更新一个有M2M连接的字段 例如:Python 通过django检查MyModel.m2m_字段中的更新方法,python,django,Python,Django,我想更新一个有M2M连接的字段 例如: class SelectMobile(models.Model): #others imei = models.ManyToManyField(Imei) status = models.BooleanField(default=True) 这是我的Imei模型 class Imei(models.Model): imei = models.CharField(max_length=15,verbose_name='IMEI
class SelectMobile(models.Model):
#others
imei = models.ManyToManyField(Imei)
status = models.BooleanField(default=True)
这是我的Imei模型
class Imei(models.Model):
imei = models.CharField(max_length=15,verbose_name='IMEI',unique=True)
mobile = models.ForeignKey(MobileModels,on_delete=models.CASCADE)
active = models.BooleanField(default=True)
只有当SelectMobile
的实例已创建且其状态
字段等于True时,我才需要在Imei
中更新活动
def update_imei_m2m(sender,instance,**kwargs):
if instance.status == True: #this doesnt work , now it works on `Imei` instead of `SelectMobile`
Imei.objects.filter(pk__in=kwargs.get('pk_set')).update(active=True)
m2m_changed.connect(update_imei_m2m,sender=SelectMobile.imei.through)
谢谢你的帮助