Django view creates new object instead of replacing it
I wrote a function like this which takes care of user payment information:
def PaymentView(request):
template = loader.get_template('reg/payment.html')
payment = PaymentInfo.objects.get(user=request.user)
if request.method == 'GET':
if payment and payment.isPaid:
return redirect('/dashboard')
return HttpResponse(template.render(request=request))
if request.POST.get('proceed'):
return redirect('/dashboard')
else:
payment = PaymentInfo(user=request.user)
price = 252000
order_id = jdatetime.datetime.now().isoformat().replace(":", "").replace(".", "").replace("-", "")
hyperlink = proceed_to_payment(payment, order_id, price)
return redirect(hyperlink)
and this is the object that it creates for users every time its called:
class PaymentInfo(models.Model):
id = models.AutoField(primary_key=True)
user = ForeignKey(User, on_delete=models.CASCADE)
isPaid = BooleanField(default=False)
payment_number = CharField(max_length=32, null=True, default=None)
order_id = CharField(max_length=32, null=True, default=None)
track_id = CharField(max_length=50, null=True, default=None)
card_no = CharField(max_length=16, null=True, default=None)
hash_card_no = CharField(max_length=64, null=True, default=None)
payment_date = DateField(auto_now_add=True, null=True)
payment_verification_date = DateField(auto_now_add=True, null=True)
it works just fine but imagine if a user clicks a certain button to get redirected to the payment page and cancel that payment and repeat this one more time. now the user has two seperate paymentInfo object. I want the first one to be updated with new info. what statement should I add to the views functiona? something like if payment.exists(), delete it or replace it some how!?