Reputation: 373
Model.py
class Customers(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
contact = models.BigIntegerField(unique=True)
amount = models.BigIntegerField()
type = models.CharField(max_length=1)`
Form.py
class Deposit_Form(forms.Form):
amount = forms.IntegerField(min_value=200, label="Enter amount u want to deposit")
View.py
class Log_in(TemplateView):
template_name = 'login.html'
def get(self, request, *args, **kwargs):
Form = Login()
return render(request, self.template_name, {'form': Form})
def post(self, request):
username = self.request.POST.get('username').strip()
password = self.request.POST.get('password').strip()
if username is None or password is None:
return HttpResponse({'Doesnt exist'}, status=HTTP_400_BAD_REQUEST)
user = authenticate(username=username, password=password)
if not user:
return HttpResponse({'Invalid candidate '}, status=HTTP_404_NOT_FOUND)
return redirect('customer_menu')
def customer_menu(request):
if "user_id" in request.session:
return render(request, 'customer_menu.html')
class Deposit(TemplateView):
template_name = 'deposit.html'
def get(self, request, *args, **kwargs):
Form = Deposit_Form()
return render(request, self.template_name, {'form': Form})
@staticmethod
def post(request):
if "user_id" in request.session:
data = request.POST.get
try:
deposit_amount=data('deposit_amount')
customer = CustomerModel(
amount = deposit_amount+amount
)
customer.save()
return HttpResponse('action performed successfully', 500)
except Exception as e:
return HttpResponse("failed : {}".format(e), 200)
now i can't understand how can deposit amount, it show error in deposit class post function amount = deposit_amount + amount
Upvotes: 1
Views: 466
Reputation: 2627
If I understand your question correctly, you want to increase your amount field on your Customer model. But firstly you must find your object which you want to update. And you can not directly CustomerModel(amount=deposit_amount+amount)
.
You can use F()
function for this. For example: CustomerModel.filter(pk=id).update(amount=F('amount')+deposite_amount)
You can find detail here for detail about F() function
Edit:
As another way, you can find your CustomerModel object with get()
method and increase your amount field. After, you can save your object.
Like that:
customer = CustomerModel.objects.get(pk=id) #you can change query field
customer.amount = customer.amount + data('deposite_amount')
customer.save()
Upvotes: 1
Reputation: 301
What is the error?
make sure that this fields can be null
user = models.ForeignKey(User, on_delete=models.CASCADE)
contact = models.BigIntegerField(unique=True)
amount = models.BigIntegerField()
type = models.CharField(max_length=1)`
Upvotes: 0