[Answered ]-I want to give users ten coins each time they fill out one form

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

Leave a comment