Changing status on M2M instances

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')

Are you looking for it to be “0” if there are any number of related items? You want it to be “1” if there are no related items?

If that’s the case, then you don’t want to maintain a separate status field - that’s replicated data in the database that is potentially circumvented (not every potential insert or delete is going to trigger a signal.) or possibly creating a race condition.

What you want to do is something like a item.location_set.count() > 0 or item.location_set.exists() to determine whether or not there are any locations currently related to the item.