Home > Back-end >  TypeError at / save() missing 1 required positional argument: 'self'
TypeError at / save() missing 1 required positional argument: 'self'

Time:03-30

I cannot update the balance amount, when I try to save the value, it raises that error.

models.py,

class balance_data(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    total_amount = models.FloatField(max_length=15)

    def __str__(self):
        return str(self.user)

Views.py,

def cash_in(request, amount):

    new_amount = request.user.balance_data.total_amount   amount

    balance_data.total_amount = new_amount
    balance_data.save()

    return HttpResponse(request.user.balance_data.total_amount)

How can I solve this?

I am expecting to update the amount.

CodePudding user response:

Views.py

def cash_in(request, amount):
    # user whose total_amount is to be updated.
    user = request.user

    # get current user instance of balance_data
    user_current_balance = balance_data.objects.get(user=user)

    # update total_amount by adding amount to its total_amount
    user_current_balance.total_amount = user_current_balance.total_amount   amount

    # save changes to database
    user_current_balance.save()
    return HttpResponse(user_current_balance.total_amount)
  • Related