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?
Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.
A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: A user needs to assign multiple categories to a blog post. A user wants to add multiple blog posts to a publication.
The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set, for instance User.
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.
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
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