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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:34:28+00:00 2026-05-20T18:34:28+00:00

I have 3 model classes: class Team(models.Model): name = models.CharField(max_length=100, default=, blank=True, null=True) number

  • 0

I have 3 model classes:

class Team(models.Model):
    name = models.CharField(max_length=100, default="", blank=True, null=True)
    number = models.IntegerField() 

class Position(models.Model):
    match = models.ForeignKey('Match')
    color = models.CharField(max_length=5)
    number = models.IntegerField()

    team = models.ForeignKey('Team')

class Match(models.Model):
    type = models.CharField(max_length=3)
    number = models.IntegerField()

    red_score = models.IntegerField(default=0)
    blue_score = models.IntegerField(default=0)

    match_events = models.TextField(max_length=1000)

Using the models as they are right now, how would I able to get a list of Matches a Team has won (i.e. if the Team belongs to a red Position, add it to the list if its Match has a red_score > blue_score)

Sorry if that’s confusing. I can try to clarify if you have any specific questions.

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. Editorial Team
    Editorial Team
    2026-05-20T18:34:29+00:00Added an answer on May 20, 2026 at 6:34 pm

    Simplest way to do this:

    Match.objects.filter(position__color='red', red_score__gt=F('blue_score'))
    

    You may want to move Position model to the bottom, to remove apostrophes from foreign key related models names.

    This is as well very good example to use ManyToMany relation:

    class Team(models.Model):
        name = models.CharField(max_length=100, default="", blank=True, null=True)
        number = models.IntegerField()
    
    
    class Match(models.Model):
        type = models.CharField(max_length=3)
        number = models.IntegerField()
        red_score = models.IntegerField(default=0)
        blue_score = models.IntegerField(default=0)
        match_events = models.TextField(max_length=1000)
        teams = models.ManyToManyField(Team, through='Position',
                                       related_name='matches')
    
    
    class Position(models.Model):
        match = models.ForeignKey(Match)
        color = models.CharField(max_length=5)
        number = models.IntegerField()
        team = models.ForeignKey(Team)
    

    In this case, you will get few more options to simplify data access, e.g.:
    if team is your actual team and match single match selected somewhere earlier in the code, this is valid:

    team.matches.filter(red_score__gt=F('blue_score')) # all won matches of this team
    
    match.teams.all() # teams involved in this match
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
Here two of my model classes: class DashboardVersion(models.Model): name = models.CharField(_(Dashboard name),max_length=100) description =
I have a four classes: class A(models.Model): field1 = models.BooleanField(default = True) class Meta:
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 one-to-many (models.ForeignKey) relationship between two of my model classes: class
In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes
Let's say I have two models, Classes and People. A Class might have one
suppose I have a domain classes: public class Country { string name; IList<Region> regions;
I have two simple Model classes and a ViewModel... public class GridItem { public
I have a couple model classes like so: public class MyModelBase { public string

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.