Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:20:03+00:00 2026-06-13T01:20:03+00:00

This question is about RelatedManager.add() which is used to add model object instances to

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:20:04+00:00Added an answer on June 13, 2026 at 1:20 am

    You can just expand your array as varargs:

    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')
    
    jk_rowling.books.add(*book_objects)   # <---------------
    
    jk_rowling.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've this question about pass some instances by ref or not: here is my
I asked this question about multithreading in servlet, and many people suggest using a
I came across this question about memory management of dictionaries, which mentions the intern
I have asked this question about using the a Linq method that returns one
In similar manner to this question about generics : In which programming language did
In relation with this question about how to write long SQL queries within C#.,
( This question about refactoring F# code got me one down vote, but also
I just came across this question about initializing local variables. Many of the answers
Similar to this question about NSubstitute , I want to know if one is
I have this question about c# language's dynamic binding behavior. Consider the following object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.