Home > Blockchain >  count the number of times an order has been approved
count the number of times an order has been approved

Time:03-22

I have an application that deals with users making an order but anytime an order is made it needs to be approved before it can be issued or sold out. Is there a way to count the number of times an order has been approved?

models

class Order(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    pro_name = models.ForeignKey(Product, on_delete=models.CASCADE, null=True,related_name='product') 
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    order_quantity = models.PositiveIntegerField(null=True)
    order_status = models.IntegerField(default=0)
    timestamp = models.DateTimeField(auto_now_add=False, auto_now=True, null=True)

views.py

def order_approve(request, order_id):
    order = Order.objects.get(id=order_id)
    order.order_status = 1
    order.save()
    return redirect('dashboard-order')

I will be grateful for your response. Thank you

CodePudding user response:

If you want to get the total number of approved orders, you can simply write this query:

count = Order.objects.filter(order_status=1).count()

If, on the other hand, you want to count how many times a single order has been marked as approved (it's not clear to me which of the two options you want from the question), add a counter to the model and update it in the view:

models.py

class Order(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    pro_name = models.ForeignKey(Product, on_delete=models.CASCADE, null=True,related_name='product') 
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    order_quantity = models.PositiveIntegerField(null=True)
    order_status = models.IntegerField(default=0)
    approved_count = models.IntegerField(default=0)
    timestamp = models.DateTimeField(auto_now_add=False, auto_now=True, null=True)

views.py

from django.db.models import F

def order_approve(request, order_id):
    order = Order.objects.get(id=order_id)
    order.order_status = 1
    order.approved_count = F('approved_count')   1
    order.save()
    return redirect('dashboard-order')
  • Related