我有以下(剥离)代码:
# games/models.py
side_choices = [('A', 'Attack'), ('D', 'Defense')]
position_choices = [(0, 'Commander'), (1, 'Knight'), (2, 'Mage'), (3, 'Healer')]
class Game(models.Model):
users = models.ManyToManyField(User, through='GameParticipation)) // User is Django's user
class GameParticipation(models.Model):
user = models.ForeignKey(User)
game = models.ForeignKey(Game)
side = models.CharField(choices=side_choices)
position = models.PositiveSmallIntegerField(choices=position_choices)
# games/forms.py
class JoinForm(forms.Form):
side = forms.ChoiceField(choices=side_choices)
position = forms.ChoiceField(choices=position_choices)
我不知道如何编写代码来获取游戏中的可用空间列表,并将其传递给JoinForm
,以便用户看不到占用空间的选项。
答案 0 :(得分:0)
这里需要更多信息,但假设您只知道在数据库中使用的空格,可能会找出哪些是免费的。
#assuming you know the game and user
game=1
user=2
#get all used spaces
usedSpaces=GameParticipation.objects.filter(game__get=game).filter(user__get=user)
#figure out from size of board, which spaces are empty
或者如果您知道空格具有空值
emptySpaces=GameParticipation.objects.filter(game__get=game).filter(user__get=user).filter(position__exact=None)