我有一个循环的按钮,范围从1-100(这可能是1-50),如果用户选择任何按钮,它填充输入字段,例如32,47,84,69。
所以我有一个django视图,将其保存到数据库,但它以这种格式保存在数据库中[u'32',u'47',u'84',u'69']我知道会被视为一个实体,但我需要将这些数字视为单独的实体,以便数据库中的任何数字都不会被任何其他用户选中。
def game_details(request, id):
template_name = 'app_agent_games/game_details.html'
get_game_id = get_object_or_404(gamesModel, id=id)
context = {
'game_details': get_game_id,
'range': range(1, get_game_id.people + 1)
}
if request.POST:
guesses = request.POST.get('guessed_number', False)
splited = guesses.split(',')
counter = request.POST.get('count_guess', False)
update_game = PlayGameForm(request.POST)
obj = PlayGame()
obj.guessed_number = splited
obj.total_stake = int(get_game_id.amount) * int(counter)
obj.game_id = get_object_or_404(gamesModel, id=id)
obj.agent_id = get_object_or_404(CustomAgent, user_id=request.user.user_id)
obj.save()
return HttpResponseRedirect('/games/details/'+id)
return render(request, template_name, context)
模型结构:
class PlayGame(models.Model):
agent_id = models.ForeignKey(CustomAgent, related_name='agent_game')
game_id = models.ForeignKey(gamesModel, related_name='agent_game_id')
guessed_number = models.CharField(max_length=100)
total_stake = models.IntegerField(default=0)
保存列表的模型字段是guessed_number
答案 0 :(得分:0)
听起来你需要一个稍微进化的数据模型:
class PlayGame(models.Model):
agent_id = models.ForeignKey(CustomAgent, related_name='agent_game')
game_id = models.ForeignKey(gamesModel, related_name='agent_game_id')
total_stake = models.IntegerField(default=0)
class GameNumber(models.Model):
game = models.ForeignKey(PlayGame)
number = models.IntegerField()
user = models.ForeignKey('YourUser', blank=True, null=True)
然后,例如,您为游戏创建所有数字1-100
,如果用户猜对了,请将用户FK设置为猜对的用户,否则保持用户FK为空。