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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:58:17+00:00 2026-05-30T10:58:17+00:00

It’s probably more a generic question about database, than a django one but let’s

  • 0

It’s probably more a generic question about database, than a django one but let’s go.

I have very often what could be considered as 2-to-many relations.
For example, I have a class Match in my project, which is an encounter between two teams.

At first, I was using many-to-many:

Match(Model):
  teams = ManyToManyField('Team', related_name='matches') #Always two teams

It ended up being very inefficient for match related pages. Especially in django 1.3, because there is no equivalent to select_related for many_to_many relation. And it’s a bit painful having to iterate when you now there is exactly two elements.

Then I switched to this model :

Match(Model):
  teams = ManyToManyField('Team', related_name='matches') #Always two teams
  team1 = ForeignKey('Team')
  team2 = ForeignKey('Team')

When I want to display match related pages, I can use select_related and display very efficiently the two teams.
And when I’m on the team page, I can follow the “matches” relation to get all the matches as before.
But I find that 3 fields to deal with one relation is totally horrible.

Am I doing it right ? What would you recommend ?

  • 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-30T10:58:19+00:00Added an answer on May 30, 2026 at 10:58 am

    I see few solutions there.

    1.

    If you’re not anticipating that in future a match will be played between other number of teams than two it may be best for you to just remove a ManyToMany field from your second design:

    Match(Model):
      team1 = ForeignKey('Team')
      team2 = ForeignKey('Team')
    

    It’ll remove a redundation in a database and makes Match modifications easier (you don’t have to worry about cohesion between team field and corresponding team1, team2 fields). It also makes impossible to create a Match with other number of corresponding teams than two, which leads to database consistency.

    On the other hand you’re losing a flexiblity and it’s harder to write queries, because of two fields to check instead of one.

    2.

    You may want to use a cache. In fact – those two fields you have are nothing other than that, but moving it to a dedicated cache database can make it more efficient and separates your main database from cached values, which leads to a clearer database design.

    3.

    Write your own ManyToMany fetcher. It’s not very hard to do, but rather ugly. You have to select teams you’re interested in and then somehow attach them to Match objects, e.g.:

    matches # Match QuerySet
    related_teams = Team.objects.filter(match__in=matches)
    matches_map = {}
    for t in related_teams:
        matches_map[t.match_id]= matches_map.get(t.match_id, []).append(t)
    
    for m in matches:
        m.fetched_teams = matches_map[m.pk]
    

    3.5.

    New version of Django will have a prefetch_related() method which is meant to work with ManyToMany queries, but I suppose you don’t have so much time.

    • 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’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is 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.