I want to update m2m field depend on the its parent model for example:
class ModelA(models.Model):
status = models.BooleanField(default=True)
status_of_product = models.ManyToManyField(Product, verbose_name='product')
class Product(models.Model):
active = models.BooleanField(default=False)
products = models.CharField(max_length=30, unique=True)
Now this makes a separate table named modela_status_of_product.
I want to update active
field in Product
only when if status
field in ModelA
equal to True
.
def update_product_m2m(sender, instance, **kwargs):
if instance.status == True: #this is wrong and doesnt work
Product.objects.filter(pk__in=kwargs.get('pk_set')).update(active=True)
m2m_changed.connect(update_product_m2m, sender=ModelA.status_of_product.through)
Is it possible please? Thanks for helping.