玩家与团队的关系

时间:2019-07-05 14:23:22

标签: django django-models

我正在建立一个数据库来显示在线游戏的比赛。我有三个问题;

第一个,我应该使用哪种方法

class Player(models.Model):
        team=models.ForeignKey(Team,related_name='player',verbose_name='Team')

OR

 class Team(models.Model):
        player=models.ManyToManyField(Player)

第三;我在球队和球员之间的桌子看起来正确吗? 持续;在这些表之后,我将在比赛和锦标赛之间建立表。比赛和球队之间应该是什么关系

class OnlineGame(models.Model):
    game_name=models.CharField(max_length=120)


class Team(models.Model):
    name=models.CharField(max_length=255,verbose_name="Takım ismi")
    slug=models.SlugField(max_length=120,unique=True)
    bio=models.TextField()
    country=models.CharField(max_length=50)
    logo=models.ImageField(null=True,blank=True,upload_to='team')
    background=models.ImageField(null=True,blank=True,upload_to='team')
    extra=models.CharField(null=True,blank=True,max_length=150)
    website=models.CharField(null=True,blank=True,max_length=120)
    game=models.ManyToManyField(OnlineGame)#manytomany because team have one or more online game team (for example sk gaming have lol and counter-strike team

    def get_unique_slug(self):
        slug=slugify(self.name.replace('ı','i'))
        unique_slug=slug
        counter=1
        while Team.objects.filter(slug=unique_slug).exists():
            unique_slug='{}-{}'.format(slug,counter)
            counter+=1
        return slug

    def __str__(self):
        return self.team_name


class PlayerGameRole(models.Model):
    role=models.CharField(max_length=50)

class Player(models.Model):
    slug=models.SlugField(unique=True,max_length=120)
    nickname=models.CharField(max_length=120)
    first_name=models.CharField(max_length=120)
    last_name=models.CharField(max_length=50)
    birthday=models.DateField(null=True,blank=True)
    picture=models.ImageField(null=True,blank=True,upload_to='player')
    country=models.CharField(max_length=50)
    role=models.ManyToManyField(PlayerGameRole)
    team=models.ForeignKey(Team,related_name='player',verbose_name='Team')
    twitch=models.URLField(null=True,blank=True)
    facebook=models.URLField(null=True,blank=True)
    twitter=models.URLField(null=True,blank=True)
    extra=models.CharField(max_length=150)
    game=models.ManyToManyField(Game)

    def get_unique_slug(self):
        slug=slugify(self.nickname.replace('ı','i'))
        unique_slug=slug
        counter=1
        while Player.objects.filter(slug=unique_slug).exists():
            unique_slug='{}-{}'.format(slug,counter)
            counter+=1
        return slug


    def age(self):
        import datetime
        return int((datetime.date.today() - self.birthday).days / 365.25)

1 个答案:

答案 0 :(得分:0)

我认为,如果玩家可以同时在几个团队中,则应该在Player模型中的Team字段中使用ManyToMany字段类型。比赛与球队之间的关系也应该是ManyToMany,因为一个球队可以参加数场比赛,而比赛可以由多支球队进行。