How to model tournaments database into a SQL in django

对着背影说爱祢 提交于 2020-01-06 06:38:17

问题


I want to model a tournament database to store data of online games My question is: How to create a model in relationship database to store all this types of tournaments? (such as, league of legends tournament, dota 2 tournament) For example, a tournament can have 8 teams or 5 teams.

This is the sketch I created in my mind. What things do you suggest (especially I need help with relationships of tables). Also how to keep team 1 and team 2 in the match table (such as, scores, winner, loser)

i thought; Game database

game_id,name

Player database

player_id,name,surname,country,Game(FK).. ( and some other fields)

Team database

team_id,name,country,game,Player(ManyToMany).. ( and some other fields)

Match database

match_id,name,match_game,match_map,team1,team2,winner,loser,date,duration,score1,score2.. ( and some other fields)

Tournament database

tournament_id,tournament_name,tournament_game,Match(ManyToMany).. ( and some other fields)

回答1:


You can create something like this in [app_name]/models.py

from django.db import models


class Tournament(models.Model):
    name = models.CharField(max_length=255)


class Team(models.Model):
    name = models.CharField(max_length=255)


class Player(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    country = models.CharField(max_length=255)
    team = models.ForeignKey(Team, on_delete=models.CASCADE)


class Match(models.Model):
    name = models.CharField(max_length=255)
    match_game = models.CharField(max_length=255)
    match_map = models.CharField(max_length=255)
    match_teams = models.ManyToManyField(Team)
    winner = models.ForeignKey(Team, on_delete=models.CASCADE)
    loser = models.ForeignKey(Team, on_delete=models.CASCADE)
    duration = models.DurationField()
    winning_score = models.PositiveIntegerField()
    losing_score = models.PositiveIntegerField()
    tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE)


class Game(models.Model):
    name = models.CharField(max_length=255)
    match = models.ForeignKey(Match, on_delete=models.CASCADE)

Some things to note:

  • You do not need to create ID fields, Django does this for you automatically.
  • A Many-to-Many field can often be replaced with a One-to-One field on the other model, for instance instead of many matches having many games, each game is part of one match. This may or may not work in your particular use case.
  • I have changed some field names (such as score_1 being replaced with winning_score) because I feel they are more clear, assuming I have correctly understood their purpose.
  • There are some fields (Tournament.tournament_game, Player.country) for which I used CharField but would be better served with a ForeingKey field to a separate model.

This also assumes that you do not need different fields for different types of tournament (League of Legends, DOTA). If you do need this you could achieve it with different models that inherit from an abstract base class:

class Game(models.Model):
    name = models.CharField(max_length=255)
    match = models.ForeignKey(Match, on_delete=models.CASCADE)

    class Meta:
        abstract = True


class DOTA2Game(Game):
    dota_field = models.CharField(max_length=255)


class LeagueOfLegendsGame(Game):
    lol_field = models.CharField(max_length=255)

In this example DOTA2Game and LeagueOfLegendsGame both inherit from Game and therefore have both a name and a match field as well as their custom fields. Setting abstract = True in the meta class of Game prevents it existing as a separate table within the database.



来源:https://stackoverflow.com/questions/56888493/how-to-model-tournaments-database-into-a-sql-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!