Home > Enterprise >  How can I add to my investment balance with django
How can I add to my investment balance with django

Time:06-16

With the code below, I'm attempting to add the amount to my current balance but I am getting a 'QuerySet' object has no attribute 'balance'

def create_investment_view(request):
    if request.method == "POST":
        form = InvestmentForm(request.POST)
        if form.is_valid():
            amount = form.cleaned_data['amount']
            user_investment = Investment.objects.all()
            user_investment = user_investment.balance   amount
    else:
        form = InvestmentForm()
    context = {
        'form':form,
    }
    return render (request, 'create-investment.html', context)





class Investment(models.Model):
    balance = models.IntegerField()
class InvestmentForm(forms.Form):
    amount = forms.IntegerField(widget=forms.TextInput(attrs={'required':True, 'max_length':80, 'class' : 'form-control', 'placeholder': ' 1,000,000'}), label=_("Amount to deposit (Ksh.)"), error_messages={ 'invalid': _("This value must contain only numbers.") })```

CodePudding user response:

Right now you are trying to add a balance to your whole query string. If there is only one user investment, the. You could do

user_investment = Investment.objects.all()[0]

Otherwise you need to use .get() and pull the user_investment that you are looking for, maybe

user_investment = Investment.objects.get(user=user)

  • Related