Django - 限制用户查询

时间:2014-03-17 15:18:39

标签: python django django-admin

我想限制用户,以便他们只能看到他们团队中的玩家。这样做的最佳方法是什么?

class Team(models.Model):
    team_name = ...
    # Whatever other attributes.

class Player(models.Model):
    first_name = ...
    last_name = ...
    # Assign players to a team.
    team = models.ForeignKey(Team)
    # Whatever stats you want to keep on the players

1 个答案:

答案 0 :(得分:1)

player.team.player_set.all()

如果每个用户都是玩家:

class Player(models.Model):
    user = models.OneToOne(User)
    first_name = ...
    last_name = ...
    # Assign players to a team.
    team = models.ForeignKey(Team)
    # Whatever stats you want to keep on the players

并在您看来:

request.user.player.team.player_set.all()