I am trying to prepopulate many2many field on saving the model through another many2many field in the same model:
class CommissionReport(models.Model):
...
law = models.ManyToManyField('Law', blank=True, null=True)
categories = models.ManyToManyField('LawCategory', blank=True, null=True)
...
The Law model has category field which is Many2Many to LawCategory and im trying to catch it and add those categories to the categories of the CommissionReport model. So im using signal and a method, here it is the code :
@staticmethod
def met(sender, instance, action, reverse, model, pk_set, **kwargs):
if action == 'post_add':
report = CommissionReport.objects.get(pk=instance.pk)
if report.law:
for law in report.law.all():
for category in law.categories.all():
print category
report.categories.add(category)
report.save()
m2m_changed.connect(receiver=CommissionReport.met, sender=CommissionReport.law.through)
It actually prints the correct categories but doesn`t add them or save them into the model.
Thanks in advance.