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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:34:48+00:00 2026-06-05T12:34:48+00:00

On my website any user have only one group. And any user can change

  • 0

On my website any user have only one group. And any user can change his group.
So it’s made by

user.groups.clear() 

and

user.groups.add(new_group)

But it’s not efficient, because there is a two SQL query: DELETE, INSERT.
How can I change group by just UPDATE query?

  • 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-05T12:34:50+00:00Added an answer on June 5, 2026 at 12:34 pm

    User and Group are related to each other using a ManyToManyField. That means an intersection table exists relating both entities, and if you don’t specify a model to map to it (using the through attribute) Django creates one for you. Looking at the sources for django.contrib.auth.models I see that’s the case.

    Fortunatly, you can access that intermediary model using the through attribute of the manager (in this case, User.groups.through). Then you can use it just like any regular model. Example:

    >>> alice = User.objects.create_user('alice', 'alice@example.com', 'alicepw')
    >>> employee = Group.objects.create(name='employee')
    >>> manager = Group.objects.create(name='manager')
    
    >>> alice.groups.add(employee)
    >>> alice.groups.all()
    [<Group: employee>]
    
    >>> alice_group = User.groups.through.objects.get(user=alice)
    >>> alice_group
    <User_groups: User_groups object>
    >>> alice_group.group = manager
    >>> alice_group.save()
    
    >>> alice.groups.all()
    [<Group: manager>]
    >>>
    

    (newlines added for readability)

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

Sidebar

Related Questions

Im building a social networking website. A user can have pals and be able
i have a website page that contains only data of which one field gives
In my website that I am building, when the user enters any value with
Are there any popular scripts to send invitations to join a website, to user's
I haven't seen any website do this properly... If I have two select fields,
I want to get the page rank for any website. I have tried many
How do I block all connections from clients to any website? I have had
I am building a website for a comedy group which uses Facebook as one
I have a website where businesses can load items for sale. Then end users
I have a Wordpress website in which one of the categories I've stripped the

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.