Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

django – get data from db and update him – how to take the last data and update, i get the data with CartOrder.objects.last()

i want that the paid status will be true on the last order i can filter the data but how i am changing him ?

my views.py file:

def payment_done(request):
    a = CartOrder.objects.last()
    orders = CartOrder.objects.filter(paid_status=a.paid_status)
    return render(request, 'payment-success.html',{'orders':orders})

my models.py file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class CartOrder(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    total_amt = models.FloatField()
    paid_status = models.BooleanField(default=False)
    order_dt = models.DateTimeField(auto_now_add=True)

>Solution :

Try:

def payment_done(request):
    # get last order
    last_order = CartOrder.objects.last()
    
    # update status
    last_order.paid_status = True
    
    # save changes to db
    last_order.save()

    orders = CartOrder.objects.filter(paid_status=a.paid_status)
    return render(request, 'payment-success.html',{'orders':orders})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading