[Django]-How to update manytomany field in Django?

28👍

Note: This code will delete the bad ‘georfe’ author, as well as updating the books to point to the correct author. If you don’t want to do that, then use .remove() as @jcdyer’s answer mentions.

Can you do something like this?

george_author = Author.objects.get(name="George")
for book in Book.objects.filter(authors__name="Georfe"):
    book.authors.add(george_author.id)
    book.authors.filter(name="Georfe").delete()

I suspect that this would be easier if you had an explicit table joining the two models (with the “through” keyword arg) — in that case, you would have access to the relationship table directly, and could just do a .update(id=george_author.id) on it.

23👍

With the auto-generated through table, you can do a two-step insert and delete, which is nice for readability.

george = Author.objects.get(name='George')
georfe = Author.objects.get(name='Georfe')

book.authors.add(george)
book.authors.remove(georfe)
assert george in book.authors

If you have an explicitly defined through table (authors = models.ManyToManyField(Author, through=BookAuthors) then you can change the relationship explicitly on BookAuthor. A little known fact is that this Model already exists, it is generated by django automatically. Usually you should only create an explicit through model if you have extra data you wish to store (for instance the chapters a particular author wrote, in a multi-author book).

# This line is only needed without a custom through model.
BookAuthor = Book.authors.through
book_author = BookAuthor.objects.get(author=georfe, book=great_american_novel)
book_author.author = george
book_author.save()
assert george in book.authors
👤jcdyer

12👍

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

Leave a comment