简体   繁体   中英

How to classify objects chosen in a ManyToManyField?

I have the following models involved: Team , Stage , Group and LeaderBoard .

A Team play in every Stage of a tournament in a Group with other teams and I would a LeaderBoard model to store the rank of the group. I use a leaderboard outside the Group because it could be round-robin (I need it) or knockout (I don't need it). I can't just set a score field in Team because in every Stage it will reset and I need to reuse old ones.

So I think (maybe there is a better solution) I need in the LeaderBoard a sort of dictionary where all the teams I choose in the ManyToManyForm are stored with their scores.

Here my cleaned models.py file (sorry for some Italian in it):

class Stage(models.Model):  # Fase
    tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE, related_name='stages')


class Group(models.Model):  # Girone
    FORMAT_TYPES = (('Round-Robin', "All'italiana"), ('Elimination', 'Ad eliminazione'))
    stage = models.ForeignKey(Stage, on_delete=models.CASCADE, related_name='groups')
    format = models.CharField(max_length=4, choices=FORMAT_TYPES, blank=True)
    teams = models.ManyToManyField(Team)



class LeaderBoard(models.Model):
    group = models.OneToOneField(Group, on_delete=models.CASCADE, blank=True, null=True)

I think I understand what you are trying to do. If teams are going head to head you could make two new models Match and Score. Match is related back to two teams and then two Scores are related back to teams and matches.

Using this system you could report scores and matches that are inside stages and groups etc.

If I am way off on my interpretation of what you are trying to do, let me know

我认为最好的方法是设置一个名为Score的新全班,其中有两个ForeignKey,一个用于团队,一个用于LeaderBoard。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM