4
you can have one query hit less with the defaults
:
reward_amount = 50
leader_board, created = LeaderboardEntry.objects.get_or_create(
player=player,
defaults={
"golds": reward_amount,
}
)
if not created:
leader_board.golds += reward_amount
leader_board.save(update_fields=["golds"])
2
I think your problem is the get_or_create()
method so it return back a tuple with two values, (object, created)
so you have to recieve them in your code as following:
reward_amount = 50
entry, __ = LeaderboardEntry.objects.get_or_create(player=player)
entry.golds += reward_amount
entry.save()
It will work better than your actual code, just will avoid make two queries.
Of course the save()
method will hit again your database.
- [Django]-Underscore in subdomain label.. valid or not?
- [Django]-Django – Ordered Table
- [Django]-Implementing Name Synchronization and Money Transfers in Transactions Model with Account Number Input
1
You can solve this with update_or_create
:
LeaderboardEntry.objects.update_or_create(
player=player,
defaults={
'golds': F('golds') + reward_amount
}
)
EDIT:
Sorry, F
expressions in update_or_create
are not yet supported.
Source:stackexchange.com