Hello,
I have two models
class Item(models.Model):
name = models.CharField(max_length=50)
sku = models.CharField(max_length=50)
description = models.TextField(max_length=100, blank=True)
status = models.IntegerField(choices=TRUE_FALSE_CHOICES, default='1')
class Meta:
verbose_name_plural = 'Items'
def __str__(self):
return self.name
class Location(models.Model):
name = models.CharField(max_length=50)
description = models.TextField(max_length=100)
items = models.ManyToManyField(Item, blank=True)
class Meta:
verbose_name_plural = 'Locations'
def __str__(self):
return self.name
So what i’m trying to achieve is when you add items to location, status to added items should change to 0, when you remove them from the location should go back to 1.
I have used signals and came up with this in order to change the status to 0 but i dont know how to make it back to 1 after removing items from list. Any ideas ?
@receiver(models.signals.m2m_changed, sender=Location.items.through)
def items_changed(sender, instance, action, **kwargs):
if action == 'post_add':
instance.items.all().update(status='0')