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

  • SEARCH
  • Home
  • 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 7527367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:05:23+00:00 2026-05-30T04:05:23+00:00

When you pass a list of pk integers into add or remove – are

  • 0

When you pass a list of pk integers into add or remove – are the objects accessed? ie. Is there a database call for each pk?

  • 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-05-30T04:05:24+00:00Added an answer on May 30, 2026 at 4:05 am

    When you create a ManyToManyField without specifying an intermediary table (using through) Django generates a table for you. This table will only need the pks of both models, so there’s no need to select anything from the other objects in order to save the new relashionships.

    A new row will be created for each of them, possibly using many inserts, but not necessarily many database calls (a single SQL query with multiple insert commands, for instance, is possible). All the info needed for those creations (the pks of your objects) are readily available, so no need for any more database hits than necessary.

    Update: seems I was mistaken. Looking at the sources (django/db/models/fields/related.py), I saw that it performs an independent creation for each object:

    for obj_id in new_ids:
        self.through._default_manager.using(db).create(**{
            '%s_id' % source_field_name: self._pk_val,
            '%s_id' % target_field_name: obj_id,
        })
    

    Before doing that, it also checks if any of the pks supplied already existed in the database (in order to avoid duplicate entries/uniqueness constraint violations):

    vals = self.through._default_manager.using(db).values_list(target_field_name, flat=True)
    vals = vals.filter(**{
        source_field_name: self._pk_val,
        '%s__in' % target_field_name: new_ids,
    })
    new_ids = new_ids - set(vals)
    

    This check is done with a single query though…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add and remove items from a list of integers. There will
I have a list of integers or of strings and need to pass it
I am trying to pass a list of questions into a template in Play
Are there examples of how to pass a list of key_names to Model.get_or_insert() ?
If I pass an empty list into a JPA query, I get an error.
I'd like to pass a list of integers as an argument to a python
I'm trying to bind a list of integers into an SQLTemplate IN clause like
I need to pass a list of strings as parameter to a console application
Hello Sir i am pass array list through url android to php my arraylistvalues[x,y.z,....]
What is the canonical way to pass a list to a Tcl procedure? I'd

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.