Django:保存到数据库connundrun

时间:2016-09-10 10:35:46

标签: mysql django

我的观点中有以下内容完美无缺:

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)

非常感谢任何帮助,非常感谢,Alan。

2 个答案:

答案 0 :(得分:2)

那是因为您每次都在创建新对象。请改用update_or_create:https://docs.djangoproject.com/en/dev/ref/models/querysets/#update-or-create

答案 1 :(得分:1)

让我解释一下update_or_create的更多信息,在你的情况下,你想要的是如果teamselectionid存在,只需更新该对象。应该是

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

修改

默认之前的参数只是让您在数据库中找到唯一行,例如,用户可能会选择多个团队,因此只有使用user_id才能找到唯一的行,但是您可以结合user_id和team_id,如:

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