Home > Enterprise >  django - get data from db and update him - how to take the last data and update, i get the data with
django - get data from db and update him - how to take the last data and update, i get the data with

Time:08-26

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

views.py:

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})

models.py:

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)

CodePudding user response:

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})
  • Related