This question is about RelatedManager.add() which is used to add model object instances to a many-to-one or many-to-many relation. To illustrate my question, let's say I have the following two models:
class Book(models.Model):
title = models.CharField()
class Author(models.Model):
name = models.CharField()
books = models.ManyToManyField(Book)
Now, for each Author
I have a list of Book
objects that I want to add to the relation. At the moment, I'm doing it like this:
book_objects = [
<Book: Harry Potter and the Philosopher's Stone>,
<Book: Harry Potter and the Chamber of Secrets>,
<Book: Harry Potter and the Prisoner of Azkaban>
]
jk_rowling = Author.objects.get(name='J. K. Rowling')
for book in book_objects:
jk_rowling.books.add(book)
jk_rowling.save()
However, this is not very efficient as I have to deal with thousands of objects to be added to a relation of this kind and it takes ages. Rather, the Django documentation recommends to call add()
with more than one model object as an argument. That is, I would have to do it like the following:
jk_rowling.books.add(book1, book2, book3)
But I cannot do this because I don't know the number of objects added to the relation beforehand. I don't understand why I can't do the following:
jk_rowling.books.add(book_objects)
Why doesn't add()
accept an iterable as an argument? And how can I add a large amount of objects to a relation like this more efficiently than calling add()
for each object separately? Thanks a lot!