2016-04-30 60 views
1

我在模型的兩個概念之間猶豫不決。Django:ManyToMany,誰最有效率?

在我的網站上,有足球隊。這些團隊包括輔導,球員,董事...等

目前,我模仿這個一樣,(1):

class Team(models.Model): 
    name = models.CharField(max_length=25,primary_key=True) 

class Chief(models.Model): 
    created_at = models.DateField(auto_now_add=True) 
    team = models.ForeignKey(Team, null=False,on_delete=models.CASCADE) 
    user = models.ForeignKey(User, null=False,on_delete=models.CASCADE) 

class Player(models.Model): 
    created_at = models.DateField(auto_now_add=True) 
    team = models.ForeignKey(Team, null=False,on_delete=models.CASCADE) 
    user = models.ForeignKey(User, null=False,on_delete=models.CASCADE) 
    position = models.CharField(max_length=30) #3 positions posible 

class Director(models.Model): 
    created_at = models.DateField(auto_now_add=True) 
    team = models.ForeignKey(Team, null=False,on_delete=models.CASCADE) 
    user = models.ForeignKey(User, null=False,on_delete=models.CASCADE) 

問題是,當我想獲得一個小組的所有成員(酋長,導演和玩家),我必須執行3個請求。 但優點是當我只想要所有的導演時,我只是在導演實體中搜索!

我毫不猶豫地刪除這3種型號(導演,球員兼),使3個關係多對多的團隊一樣,(2):

class Team(models.Model): 
    name = models.CharField(max_length=25,primary_key=True) 
    chiefs = models.ManyToManyField(User) 
    directors = models.ManyToManyField(User) 
    players_position1 = models.ManyToManyField(User) 
    players_position2 = models.ManyToManyField(User) 
    players_position3 = models.ManyToManyField(User) 

哪家的最有效的方式(1)和(2)?

回答

0

您可以使用model inheritance

models.py

class TeamMember(models.Model): 
    created_at = models.DateField(auto_now_add=True) 
    team = models.ForeignKey(Team, null=False,on_delete=models.CASCADE) 
    user = models.ForeignKey(User, null=False,on_delete=models.CASCADE) 

class Chief(TeamMember): 
    pass 

class Director(TeamMember): 
    pass 

class Player(TeamMember): 
    position = models.CharField(max_length=30) 

views.py

#Get all members 
members = TeamMember.objects.all() 

#Only players 
players = Player.objects.all() 
+0

但是我會重複很多數據嗎?因爲TeamMember不是抽象的,所以這個模型將包含像Chief模型一樣的信息,例如... – Zoulou

+0

編號表 _chief將只有一列 - teammeber_ptr。 –

+0

啊謝謝你,我不知道! – Zoulou

0
class Team(models.Model): 
    name = models.CharField(max_length=25,primary_key=True) 

class MemberQuerySet(models.QuerySet): 
    def chiefs(self): 
     return self.filter(position='a') 

    def directors(self): 
     return self.filter(position='b') 

    def players(self): 
     return self.filter(position__in=['c', 'd', 'e']) 

class Member(models.Model): 
    created_at = models.DateField(auto_now_add=True) 
    team = models.ForeignKey(Team, models.CASCADE, related_name="members") 
    user = models.ForeignKey(User, models.CASCADE) 
    position = models.CharField(max_length=1, choices=(
     ('a', 'chief'), 
     ('b', 'director'), 
     ('c', 'player position 1'), 
     ('d', 'player position 2'), 
     ('e', 'player position 3')) 

    objects = MemberQuerySet.as_manager() 

現在,你可以得到一個團隊的所有成員或只是在數據庫中只有一個命中的球員

members = Team.objects.get(pk='someteam').members 
players = Team.objects.get(pk='someteam').members.players() 
+0

謝謝您的解決方案,我會將其與Sergey Gornostaev的解決方案進行比較!謝謝 ! – Zoulou

+0

您忘記了與會員模型的多種關係嗎? – Zoulou