Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Django:保存到数据库connundrun_Mysql_Django - Fatal编程技术网

Mysql Django:保存到数据库connundrun

Mysql Django:保存到数据库connundrun,mysql,django,Mysql,Django,我认为以下几点非常有效: if request.method == 'POST': form = SelectTwoTeams(request.POST,user=request.user) if form.is_valid(): teamSelection1 = UserSelection(campaignno = 102501350, teamselection1or2 = 1,

我认为以下几点非常有效:

if request.method == 'POST':

form = SelectTwoTeams(request.POST,user=request.user)

if form.is_valid():

    teamSelection1 = UserSelection(campaignno = 102501350,
                  teamselection1or2 = 1,
                                  teamselectionid_id = request.POST['team1'],
                                  user_id=currentUserID,
                                  fixturematchday=fixturematchday,
                                  soccerseason_id=soccerseason)
    teamSelection1.save()

    teamSelection2 = UserSelection(campaignno = 102501350,
                                   teamselection1or2 = 2,
                                   teamselectionid_id = request.POST['team2'],
                                   user_id=currentUserID,
                                   fixturematchday=fixturematchday,
                                   soccerseason_id=soccerseason)
    teamSelection2.save()
当代码运行时,它会很好地在我的数据库中结束,因为它应该:

mysql> select * from straightred_userselection;
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
| userselectionid | campaignno | teamselection1or2 | teamselectionid | user_id | fixturematchday | soccerseasonid |
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
|               9 |  102501350 |                 1 |               6 |     349 |               2 |           1025 |
|              10 |  102501350 |                 2 |               7 |     349 |               2 |           1025 |
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
2 rows in set (0.00 sec)
但是,让我们假设我现在想要选择两个不同的团队(目前在teamselectionid列下为6和7)。我可以在表单中选择它们并进行更新,但是当代码运行时,它只会向表中添加另外两行,而不是更新当前的行。检查是否需要更新的一组新团队选择,只需满足以下条件:

  • fixturematchday和soccerseasonid随后相同,然后更新
  • fixturematchday和soccerseasonid不相同,然后创建新条目
如上所述,它目前只添加了两个新行,如下所示:

mysql> select * from straightred_userselection;
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
| userselectionid | campaignno | teamselection1or2 | teamselectionid | user_id | fixturematchday | soccerseasonid |
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
|               9 |  102501350 |                 1 |               6 |     349 |               2 |           1025 |
|              10 |  102501350 |                 2 |               7 |     349 |               2 |           1025 |
|              11 |  102501350 |                 1 |               9 |     349 |               2 |           1025 |
|              12 |  102501350 |                 2 |              14 |     349 |               2 |           1025 |
+-----------------+------------+-------------------+-----------------+---------+-----------------+----------------+
4 rows in set (0.00 sec)

非常感谢您的帮助,艾伦。

这是因为您每次都在创建新对象。改用update\u或create:

让我解释一下有关update\u或create的更多信息,在您的情况下,您需要的是如果teamselectionid存在,只需更新该对象即可。因此应该

obj, created = UserSelection.objects.update_or_create(
teamselectionid=teamselectionid, defaults=what_field_you_want_to_update)
编辑 默认值之前的参数只允许您在数据库中查找唯一行,例如,一个用户可以选择多个团队,因此仅使用用户id无法找到唯一行,但您可以将用户id和团队id组合在一起,如:

obj, created = UserSelection.objects.update_or_create(
    user_id=user_id, team_id=team_id, defaults=what_field_you_want_to_update)

抱歉,我刚刚意识到如果teamselectionid不存在,则不是这样。会有很多人选择了这个团队。仅当用户id、fixturematchday和seasonid存在时才可用。希望这有意义?