gpt4 book ai didi

python - Django 更改和跟踪订单状态

转载 作者:行者123 更新时间:2023-12-05 06:14:09 24 4
gpt4 key购买 nike

我的模型中有一个状态字段,其默认值正在进行中。每当创建新订单时,都会为订单提供默认值 in-progress。现在我添加了一个新功能,当客户输入订单 ID 时,它会显示订单状态。我想在我的功能中添加新功能,当客户输入 order.id 并点击跟踪订单状态时,订单状态应自动更改为预定状态,如果我稍后更新订单状态,它不应该显示预定状态,而是我将显示的状态选择。我可以在我的 View 函数中添加什么来实现它?

models.py

class Order(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=100
status = models.CharField(max_length = 100, choices = choices) default="In Progress")
choices = (('Received', 'Received'),
('Scheduled', 'Scheduled'),
('Shipped', 'Shipped'),
('In Progress','In Progress'),
)```
views.py

def order_tracker(request):
if request.method=="POST":
orderId = request.POST.get('orderId', '')
try:
order=Order.objects.filter(pk=orderId)

if len(order)>0:
update = Order.objects.filter(pk=orderId)
updates = []
for order in update:
order.status = 'Scheduled'
order.save()
updates.append({'status' : order.status})
response = json.dumps(updates)
return HttpResponse(response)
else:
return HttpResponse('{}')
except Exception as e:
return HttpResponse('{}')
return render(request,"status/track.html")

最佳答案

def order_tracker(request):
if request.method=="POST":
orderId = request.POST.get('orderId', '')
try:
order=Order.objects.filter(pk=orderId)

if len(order)>0:
update = Order.objects.filter(pk=orderId)
updates = []
for order in update:
# change order status to scheduled
if order.status == 'processing':
order.status = 'scheduled'
order.save()
updates.append({'status' : order.status})
response = json.dumps(updates)
return HttpResponse(response)
else:
return HttpResponse('{}')
except Exception as e:
# add some logging here
return HttpResponse('{}')
return render(request,"tracker.html")

顺便说一句,您似乎想要一份订单 list ?如果您应该只获得一个订单,请使用 Order.objects.get(),如果条件不匹配,这将引发 DoesNotExist 异常。

def order_tracker(request):
if request.method=="POST":
orderId = request.POST.get('orderId', '')
try:
order=Order.objects.get(pk=orderId)
if order.status == 'processing':
order.status = 'scheduled'
order.save()
return JsonResponse({'status' : order.status})
except: Order.DoesNotExist:
return JsonResponse({})
return render(request,"tracker.html")

关于python - Django 更改和跟踪订单状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63007246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com