My Company class have multiple M2M relationship with itself
class Company(models.Model):
divisions = models.ManyToManyField('self', symmetrical=False, related_name="parent_companies")
parents = models.ManyToManyField('self', symmetrical=False, related_name="divisions_companies")
comp = models.ManyToManyField('self', symmetrical=False, related_name="comp")
friends = models.ManyToManyField('self', symmetrical=False, related_name="friends")
I would like to be able to dynamically add M2M relationship like this, but it obviously does not work. Is there a way to do it dynamically ?
company, was_created = Company.objects.get_or_create(name=info)
setattr(self,key, company)
Got it. Simply have to pass a list instead..
company, was_created = Company.objects.get_or_create(name=info)
setattr(self,key, [company,])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With