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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:18:22+00:00 2026-05-11T12:18:22+00:00

for u in Users.objects.all(): for g in u.group.all(): if g not in Groups.objects.filter(domain__user=u.id): u.group.filter(id=g.id).delete()

  • 0
        for u in Users.objects.all():              for g in u.group.all():                 if g not in Groups.objects.filter(domain__user=u.id):                    u.group.filter(id=g.id).delete() 

How do I delete the entries in relationship table. In this case I have a many to many relationship between Groups and Users. The delete statement in the above code deletes the group from the Groups table. I just want to delete the relationship between the user and the group from the Users_group table. How do I do this.

Thanks

  • 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. 2026-05-11T12:18:22+00:00Added an answer on May 11, 2026 at 12:18 pm

    The key to thinking about this problem is to realize that u.group is a manager, just as Groups.objects is a manager (by default the former is a subclass of the latter). Most operations that you call on u.group will affect the entire Group table (with the possibility that it first filters down to objects related to u). That means that, assuming g is related to u,

    u.group.filter(id=g.id).delete() 

    should work the same as

    Groups.objects.filter(id=g.id).delete() 

    In both cases, .filter() returns a queryset (completely naive with respect to u) and .delete() deletes all members.

    The good news is that u.group should be a ManyRelatedManager, meaning that there will be additional methods available for it. For many examples, check here. The one that should fit your scenario:

    u.group.remove(g) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program in which the user adds multiple objects to a scene.
Suppose I have an array of a objects of user defined class. Wanted to
I'm working on a system were a user can edit existing objects (Filter domain
I have an array of objects which populate a UITableView . When a user
I have seen sample source code around that uses different ways of releasing/dealloc'ing objects,
I have a ModelForm where i give users a chance to create a new
Object-Relational-Mappers have been created to help applications (which think in terms of objects) deal
My WinForms app uses a number of BackgroundWorker objects to retrieve information from a
I'm using local shared objects within flash both to store significant amounts of user
I need a library that can detect objects in an image (uses edge detection).

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.