1👍
✅
If the User
model you show above is your AUTH_USER_MODEL
then all you really need is
def give_coins(user, count):
user.coins = F('coins') + count
user.save(update_fields=('coins',))
user.refresh_from_db(fields=('coins',))
(which is using F()
to avoid (some) race conditions); there is no .profile
you’d need to access.
You can then call this in e.g. your FormView
‘s form_valid
:
class MoneyFormView(FormView):
# ...
def form_valid(self, request):
give_coins(request.user, 10)
return super().form_valid(request)
👤AKX
Source:stackexchange.com