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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:09:25+00:00 2026-05-28T04:09:25+00:00

I have 2 models: class Team(models.Model): Team model for creating a basic team. image

  • 0

I have 2 models:

class Team(models.Model):
        """
        Team model for creating a basic team.
        """
        image = ThumbnailerImageField(upload_to=_profile_image_directory(), verbose_name=u'Logo', help_text=u'Uploading a team logo is highly recommended.', blank=True)
        name = models.CharField(verbose_name=u'Team Name', max_length=100)
        school = models.CharField(verbose_name=u'School Name', max_length=100, blank=True)
        city = models.CharField(max_length=100, blank=True)
        state = USStateField(blank=True)
        description = models.CharField(blank=True, max_length=175)
        members = models.ManyToManyField(User, through='TeamMember')
        key = models.CharField(max_length=8, unique=False, blank=True)
        private = models.BooleanField(blank=True)
        twitter = models.CharField(verbose_name=u'Twitter handle', max_length=15, blank=True)
        hashtag = models.CharField(help_text=u'Tweets containing this hashtag will be pulled into your team\'s Twitter feed', max_length=20, blank=True)

        def __unicode__(self):
                return u'%s' % (self.name)

        class Meta:
                verbose_name = u'Team'
                verbose_name_plural = u'Teams'
                unique_together = ('name', 'school', 'state')

class TeamMember(models.Model):
        """
        Team model for connecting users with teams.
        """
        user = models.ForeignKey(User)
        team = models.ForeignKey(Team)
        number = models.PositiveIntegerField(max_length=3, blank=True, null=True)
        position = models.CharField(max_length=100, blank=True)
        start = models.CharField(help_text=u'Year only. Format: 2004', max_length=4, blank=True)
        end = models.CharField(help_text=u'Year only. Leave blank for current team', max_length=4, blank=True)
        created = models.DateTimeField(auto_now_add=True)
        updated = models.DateTimeField(auto_now=True)

I’m trying to search all of the teammates of the current user by name, so I have a query here:

TeamMember.objects.filter(team__members=request.user).filter(user__username__icontains=query).exclude(user=request.user)

This query works, but I many times get duplicate users if that user is in several of the same teams as you, so I want to do a DISTINCT or GROUP BY on the user field, but everything I try isn’t working.

I’ve tried:

TeamMember.objects.filter(team__members=request.user).filter(user__username__icontains=query).exclude(user=request.user).distinct("user")

This one works, but doesn’t give me enough information about the user, just their id because it gets converted to a dict:

TeamMember.objects.filter(team__members=request.user).filter(user__username__icontains=query).exclude(user=request.user).values("user").distinct()

Any thoughts?

  • 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-28T04:09:26+00:00Added an answer on May 28, 2026 at 4:09 am

    Your query returns distinct TeamMembernot distinct user.

    In order to get distinct user:

    user.objects.filter( 
                    teammember__team__teammember__user__icontains=query
              ).exclude(user=request.user).distinct()
    

    Explained: distincts users that they teammember has a team that is related to a teammember that has a user that contains query all this excluding logged user.

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

Sidebar

Related Questions

hi i have to following model class Match(models.Model): Team_one = models.ForeignKey('Team', related_name='Team_one') Team_two =
I have the following models defined: class Team(models.Model): Name = models.CharField(max_length=250) class Fixture(models.Model): HomeTeam
I have 3 model classes: class Team(models.Model): name = models.CharField(max_length=100, default=, blank=True, null=True) number
I have a very simple method: Class Team(models.Model): def sides(self): return SideNames.objects.filter(team=self) SideNames is
I have the following models defined: class Player(models.Model): Team = models.ForeignKey(Team) Name = models.CharField(max_length=200)
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
I currently have the following models: class Player < ActiveRecord::Base belongs_to :team belongs_to :user
I have the following models: class Fixture(models.Model): id = models.AutoField(primary_key=True) home_team = models.ForeignKey(Player, related_name=home)
I have a player, game, shot, round and team model. When creating a shot,

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.