Here\'s an example:
If I have these classes
class Author(models.Model):
name = models.CharField(max_length=45)
class Book(models.Model):
nam
for django >=1.11 documentation:
>>> b = Blog.objects.get(id=1)
>>> e = Entry.objects.get(id=234)
>>> b.entry_set.add(e) # Associates Entry e with Blog b.
>>> new_list = [obj1, obj2, obj3]
>>> e.related_set.set(new_list)
This method accepts a clear
argument to control how to perform the operation. If False
(the default), the elements missing from the new set are removed using remove()
and only the new ones are added. If clear=True
, the clear()
method is called instead and the whole set is added at once.
and refrence: How to .update m2m field in django