30

Here's an example:

If I have these classes

class Author(models.Model):
    name = models.CharField(max_length=45)

class Book(models.Model):
    name = models.CharField(max_length=45)
    authors = models.ManyToManyField(Author)

In the database I have one Author with the name "George" and another one with the name "Georfe". The last one is a mistake. So what I want is in every Book that have "Georfe" as one of his Author replace it by the Author "George".

In SQL is really easy to do. If id of "George" = 3 and id of "Georfe" = 7 and the relation table name is "author_book":

UPDATE author_book SET id=3 WHERE id=7;

Is it possible to do that with the Django ORM?

I found a way to do it: I loop trough all the related Books of the mistyped author and do:

book.authors.add(Author.objects.get(id=3))
book.authors.remove(Author.objects.get(id=7))

But I don't find this solution really elegant and efficient. Is there a solution without the loop?

Etienne
  • 12,440
  • 5
  • 44
  • 50
  • If this is a one time deal to fix some typos, why not just run the raw SQL? – thedz Jul 28 '09 at 15:08
  • The data in the database is coming from an old filemaker DB and there's lot of typos to fix. I don't want to fix all the typos by myself. I wish my client could do it in the admin interface. – Etienne Jul 28 '09 at 15:18

3 Answers3

27

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.

Ian Clelland
  • 43,011
  • 8
  • 86
  • 87
  • See my comment to jcd, your solution are mostly the same (except for the delete, but I don't want to delete the mistyped author necessarily so remove is better in my case). I think your solution with the update is the proper way to do it. I accept this answer. Unfortunately my case is more complex because I extend ModelAdmin and everything have to be generic and I don't think I could use the relationship table directly (because I could not be sure if the "trough" arguemnt is set). I'm right? – Etienne Jul 28 '09 at 18:29
  • On an instance, you can see the 'through' argument: book.authors.through = 'ModelName' -- I'm not certain if you can get to it through the class itself, though. – Ian Clelland Jul 28 '09 at 19:56
  • I check the "through" argument on an instance and if it's not set in the ManyToManyField, through is None. So for the moment I will keep the solution of the loop with the add/remove (or delete). Maybe it'S not elegent and efficient but it works well enough for our needs. Thanks Ian, jcd – Etienne Jul 29 '09 at 03:26
  • 3
    The use of `.filter(...).delete()` actually deletes the author, not just the many-to-many relation. jcdyer has the correct answer: call `remove()`. – Gareth Rees Feb 23 '12 at 13:48
  • I'll update the answer to note that -- in the original question, it was stated that the 'Georfe' author was a mistake, and so I figured that the correct course was to delete that record, after updating the dependent book records. – Ian Clelland Feb 23 '12 at 16:51
  • Hey @IanClelland, can you have a look at this?? I am stuck for days https://stackoverflow.com/questions/66775919/unable-to-update-many-to-many-fields-in-django-rest-framework – Reactoo Mar 24 '21 at 09:07
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
Will S
  • 744
  • 8
  • 17
jcdyer
  • 18,616
  • 5
  • 42
  • 49
  • Ha, we write mostly the same thing at the same time! But I didn't know about the "trough" argument of ManyToManyField. I will look into that. But the way Ian use the relationship table with the update seem to be more effcient. – Etienne Jul 28 '09 at 18:20
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

user10210918
  • 121
  • 1
  • 2