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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:38:47+00:00 2026-05-19T17:38:47+00:00

I’m trying to do something simple like this: members = models.ManyToManyField(User, blank=True, null=True, unique=True)

  • 0

I’m trying to do something simple like this:

members = models.ManyToManyField(User, blank=True, null=True, unique=True)

but unique isn’t allowed. When looking at the table created, it makes foreign keys so uniqueness is implied I imagine.

I want to be able to associate members with this model representing a group. The group can have no members but I don’t want the same member to be able to join the group twice.

My thought would be that an exception would be thrown if I try and do this but it seems that an exception is not thrown.

def join(request,id):
    user = request.user
    mygroup = Group.objects.get(id=id)
    mygroup.members.add(user)
    mygroup.num_members += 1
    mygroup.save()

num_members is incremented, because no exception is thrown. Duplicate users don’t appear in the admin utility. Does add() fail silently? Should I just simply check if the user is contained already before adding?

  • 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-19T17:38:48+00:00Added an answer on May 19, 2026 at 5:38 pm

    For one, I wouldn’t use num_members. Instead, you can check how many members there are with mygroup.members.count(). Secondly, adding members more than once doesn’t really add them more than once, so you’re fine.

    A ManyToManyField on Group for member pointing to User is implemented with a separate table (something like group_group_users) which has a foreign key to Group and User. A user can have multiple groups, and a group can have multiple users, but there can’t be two rows in group_group_users for the same relationship (ie, unique together foreign keys).

    Usage:

    >>> group = Group.objects.get(pk=1)
    >>> user = User.objects.get(pk=1)
    >>> group.members.add(user)
    >>> # Worked fine as expected. Let's check the results.
    >>> group.members.all()
    [<User: foousername>]
    >>> group.members.add(user)
    >>> # Worked fine again. Let's check for duplicates.
    >>> group.members.all()
    [<User: foousername>]
    >>> # Worked fine.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need to clean up various Word 'smart' characters in user input, including but
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.