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 6229833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:38:29+00:00 2026-05-24T09:38:29+00:00

My model: class Sample(models.Model): users = models.ManyToManyField(User) I want to save both user1 and

  • 0

My model:

class Sample(models.Model):
    users = models.ManyToManyField(User)

I want to save both user1 and user2 in that model:

user1 = User.objects.get(pk=1)
user2 = User.objects.get(pk=2)
sample_object = Sample(users=user1, users=user2)
sample_object.save()

I know that’s wrong, but I’m sure you get what I want to do. How would you do it ?

  • 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-24T09:38:30+00:00Added an answer on May 24, 2026 at 9:38 am

    You cannot create m2m relations from unsaved objects. If you have the pks, try this:

    sample_object = Sample()
    sample_object.save()
    sample_object.users.add(1,2)
    

    Update: After reading the saverio’s answer, I decided to investigate the issue a bit more in depth. Here are my findings.

    This was my original suggestion. It works, but isn’t optimal. (Note: I’m using Bars and a Foo instead of Users and a Sample, but you get the idea).

    bar1 = Bar.objects.get(pk=1)
    bar2 = Bar.objects.get(pk=2)
    foo = Foo()
    foo.save()
    foo.bars.add(bar1)
    foo.bars.add(bar2)
    

    It generates a whopping total of 7 queries:

    SELECT "app_bar"."id", "app_bar"."name" FROM "app_bar" WHERE "app_bar"."id" = 1
    SELECT "app_bar"."id", "app_bar"."name" FROM "app_bar" WHERE "app_bar"."id" = 2
    INSERT INTO "app_foo" ("name") VALUES ()
    SELECT "app_foo_bars"."bar_id" FROM "app_foo_bars" WHERE ("app_foo_bars"."foo_id" = 1  AND "app_foo_bars"."bar_id" IN (1))
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 1)
    SELECT "app_foo_bars"."bar_id" FROM "app_foo_bars" WHERE ("app_foo_bars"."foo_id" = 1  AND "app_foo_bars"."bar_id" IN (2))
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 2)
    

    I’m sure we can do better. You can pass multiple objects to the add() method:

    bar1 = Bar.objects.get(pk=1)
    bar2 = Bar.objects.get(pk=2)
    foo = Foo()
    foo.save()
    foo.bars.add(bar1, bar2)
    

    As we can see, passing multiple objects saves one SELECT:

    SELECT "app_bar"."id", "app_bar"."name" FROM "app_bar" WHERE "app_bar"."id" = 1
    SELECT "app_bar"."id", "app_bar"."name" FROM "app_bar" WHERE "app_bar"."id" = 2
    INSERT INTO "app_foo" ("name") VALUES ()
    SELECT "app_foo_bars"."bar_id" FROM "app_foo_bars" WHERE ("app_foo_bars"."foo_id" = 1  AND "app_foo_bars"."bar_id" IN (1, 2))
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 1)
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 2)
    

    I wasn’t aware that you can also assign a list of objects:

    bar1 = Bar.objects.get(pk=1)
    bar2 = Bar.objects.get(pk=2)
    foo = Foo()
    foo.save()
    foo.bars = [bar1, bar2]
    

    Unfortunately, that creates one additional SELECT:

    SELECT "app_bar"."id", "app_bar"."name" FROM "app_bar" WHERE "app_bar"."id" = 1
    SELECT "app_bar"."id", "app_bar"."name" FROM "app_bar" WHERE "app_bar"."id" = 2
    INSERT INTO "app_foo" ("name") VALUES ()
    SELECT "app_foo_bars"."id", "app_foo_bars"."foo_id", "app_foo_bars"."bar_id" FROM "app_foo_bars" WHERE "app_foo_bars"."foo_id" = 1
    SELECT "app_foo_bars"."bar_id" FROM "app_foo_bars" WHERE ("app_foo_bars"."foo_id" = 1  AND "app_foo_bars"."bar_id" IN (1, 2))
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 1)
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 2)
    

    Let’s try to assign a list of pks, as saverio suggested:

    foo = Foo()
    foo.save()
    foo.bars = [1,2]
    

    As we don’t fetch the two Bars, we save two SELECT statements, resulting in a total of 5:

    INSERT INTO "app_foo" ("name") VALUES ()
    SELECT "app_foo_bars"."id", "app_foo_bars"."foo_id", "app_foo_bars"."bar_id" FROM "app_foo_bars" WHERE "app_foo_bars"."foo_id" = 1
    SELECT "app_foo_bars"."bar_id" FROM "app_foo_bars" WHERE ("app_foo_bars"."foo_id" = 1  AND "app_foo_bars"."bar_id" IN (1, 2))
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 1)
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 2)
    

    And the winner is:

    foo = Foo()
    foo.save()
    foo.bars.add(1,2)
    

    Passing pks to add() gives us a total of 4 queries:

    INSERT INTO "app_foo" ("name") VALUES ()
    SELECT "app_foo_bars"."bar_id" FROM "app_foo_bars" WHERE ("app_foo_bars"."foo_id" = 1  AND "app_foo_bars"."bar_id" IN (1, 2))
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 1)
    INSERT INTO "app_foo_bars" ("foo_id", "bar_id") VALUES (1, 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model: class Zone(models.Model): name = models.CharField(max_length=128) users = models.ManyToManyField(User, related_name='zones', null=True,
Given an object like: class M(models.Model): test = models.BooleanField() created_date = models.DateTimeField(auto_now_add=True) Given sample
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
I've quite a simple query regarding models. I have a model - class User
I have a class Book : from django.db import models from users.models import User
I have the following two simple models for users and user groups: class User
I have a very simple method: Class Team(models.Model): def sides(self): return SideNames.objects.filter(team=self) SideNames is
I have a simple Django model resembling: class Address(models.Model): blah class MemberData(models.Model): user =
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
#model class Promotion(models.Model): name = models.CharField(max_length=200) start_date = models.DateTimeField() end_date = models.DateTimeField() #view def

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.