如何在Django中将锦标赛数据库建模为SQL [英] How to model tournaments database into a SQL in django
问题描述
我想对比赛数据库进行建模以存储在线游戏的数据我的问题是:如何在关系数据库中创建模型来存储所有类型的锦标赛?(例如英雄联盟锦标赛,Dota 2锦标赛)例如,一个锦标赛可以有8个团队或5个团队.
这是我在脑海中创建的草图.您有什么建议(尤其是我需要与表之间的关系有所帮助).还有如何将第1队和第2队保留在比赛表中(例如比分,赢家,输家)
我想;游戏数据库 game_id,名称
玩家数据库
player_id,名称,姓氏,国家,游戏(FK)..(以及其他一些字段)
团队数据库
team_id,名称,国家/地区,游戏,Player(ManyToMany)..(以及其他一些字段)
匹配数据库
match_id,名称,match_game,match_map,team1,team2,获胜者,失败者,日期,持续时间,score1,score2 ..(以及其他一些字段)
比赛数据库
tournament_id,tournament_name,tournament_game,Match(ManyToMany)..(和其他一些字段)
您可以在 [app_name]/models.py
比赛类(models.Model):名称= models.CharField(max_length = 255)Team(models.Model)类:名称= models.CharField(max_length = 255)类Player(models.Model):first_name = models.CharField(最大长度= 255)last_name = models.CharField(最大长度= 255)国家=模型.CharField(max_length = 255)team = models.ForeignKey(Team,on_delete = models.CASCADE)Match(models.Model)类:名称= models.CharField(max_length = 255)match_game = models.CharField(max_length = 255)match_map = models.CharField(max_length = 255)match_teams = models.ManyToManyField(Team)获胜者=模型.ForeignKey(团队,on_delete = models.CASCADE)失败者=模型.外键(团队,on_delete =模型.CASCADE)持续时间= models.DurationField()Winning_Score = models.PositiveIntegerField()missing_score = models.PositiveIntegerField()比赛= models.ForeignKey(锦标赛,on_delete = models.CASCADE)类Game(models.Model):名称= models.CharField(max_length = 255)match = models.ForeignKey(匹配,on_delete = models.CASCADE)
一些注意事项:
- 您不需要创建ID字段,Django会自动为您创建.
- 多对多字段通常可以用另一种模型上的一对多字段替换,例如,代替具有很多游戏的许多比赛,每个游戏都是一个比赛的一部分.在您的特定用例中,这可能行不通.
- 我已经更改了某些字段名称(例如用
winning_score
替换的score_1
),因为假设我已经正确理解了它们的用途,我觉得它们更加清楚了. - 我使用了
CharField
来处理某些字段(Tournament.tournament_game
,Player.country
),但最好使用ForeingKey
字段添加到单独的模型.
这还假设您不需要为不同类型的锦标赛(英雄联盟,DOTA)使用不同的字段.如果确实需要此功能,则可以使用继承自 解决方案
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 withwinning_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 usedCharField
but would be better served with aForeingKey
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.
这篇关于如何在Django中将锦标赛数据库建模为SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!