选择多次时,如何防止订单在结帐屏幕上重复显示

时间:2018-11-20 09:45:45

标签: django python-3.x

对于我的商店,我基本上有一个带有添加项目选项的数量框。当我选择数量并单击添加项目时,该订单即被添加到我的订单列表中,并在结帐时显示。但是,假设我第一次选择订单A并将数量设置为5。第二次,我再次选择订单A并将数量设置为3,我希望它添加到先前的订单并将数量显示为8。如果是我的代码,它将显示为单独的订单。我该如何解决这个问题?

views.py

def addItem(request):
   selected_supply = Supply.objects.get(pk=request.POST['supplyID'])
   qty = request.POST['qty']
   try:
       current_order = Order.objects.filter(owner=1).get(status="pre-place")
   except Order.DoesNotExist:
       current_order = Order.objects.create(owner=Manager.objects.get(pk=1), status="pre-place")
   order_detail = OrderDetail.objects.create(orderID=current_order, supplyID=selected_supply, quantity=qty)
   return HttpResponseRedirect(reverse('Store:browse'))

models.py

class OrderDetail(models.Model): #change to orderitem
    orderID = models.ForeignKey(Order, on_delete=models.CASCADE)
    supplyID = models.ForeignKey(Supply, on_delete=models.CASCADE)
    quantity = models.IntegerField()
    def __str__(self):
        return f'{self.orderID.pk}: {self.supplyID.name}  {self.quantity}'

class Order(models.Model):
    owner = models.ForeignKey(ClinicManager, on_delete=models.CASCADE)
    priority = models.CharField(max_length=200, blank=True)
    weight = models.DecimalField(max_digits=4, decimal_places=2,  default=0.00)
    status = models.CharField(max_length=200)
    placeTime = models.DateTimeField(default=datetime.now())
    dispatchTime = models.DateTimeField(default=datetime.now())
    deliveredTime = models.DateTimeField(default=datetime.now())
    def __str__(self):
        return f'{self.pk}'
    def __lt__(self, other):
        if (self.priority == other.priority):
            return self.placeTime < other.placeTime
        elif (self.priority == "High"):
            return True
        elif (other.priority == "Low"):
            return True
        else:
            return False

0 个答案:

没有答案