Alan Tingey
Alan Tingey

Reputation: 961

Django: Saving to database connundrun

I have the following within my view which works perfectly:

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()

When the code is run it ends up nicely in my database as it should:

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)

However, lets just say I now want to choose two different teams (currently 6 & 7 under the teamselectionid column). I can select them in my form and update but when the code is run it just adds another two rows to the table rather than update the current ones. The check to see if it needs to be a new set of team selections of an update is simply if:

As mention above it currently just adds two new rows as per below:

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)

Any help would be greatly appreciated, many thanks, Alan.

Upvotes: 2

Views: 45

Answers (2)

Windsooon
Windsooon

Reputation: 7110

Let me explain more about update_or_create, in your case, what you want is if the teamselectionid existed, just update that object.So should be

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

EDIT

The parameters before defaults just let you find the unique row in your database, For instance, a user may choose many teams, so only use user_id can't find the unique row, but you can combine user_id and team_id, like:

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

Upvotes: 1

Michael Josephson
Michael Josephson

Reputation: 228

That's because you are creating new objects each time. Use update_or_create instead: https://docs.djangoproject.com/en/dev/ref/models/querysets/#update-or-create

Upvotes: 2

Related Questions