You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django中如何变更用车请求的状态(WAITING/CONFIRMED/DENIED)

Solution to Update RequestCar Situation on Approval

To update the RequestCar's situation to "CONFIRMED" when the request is approved, you'll need to modify your replyGeneralRequest view to check the approval status after saving the ResponseRequest instance, then update the linked RequestCar.

Modified views.py Code

def replyGeneralRequest(request, template_name="answer/reply_request.html"):
    form = AnswerRequestForms(request.POST or None, request.FILES or None)
    if form.is_valid():
        # Save the response and capture the saved instance
        response = form.save()
        
        # Update RequestCar situation if approved
        if response.accepted is True:
            request_car = response.request
            request_car.situation = "CONFIRMED"
            request_car.save()
        # Optional: Handle denied requests (remove if not needed)
        elif response.accepted is False:
            request_car = response.request
            request_car.situation = "DENIED"
            request_car.save()
        
        return redirect('listGeneralRequest')
    else:
        form = AnswerRequestForms()
    return render(request, template_name, {'form': form})

Key Breakdown:

  1. Capture the Saved Response: When you call form.save(), it returns the saved ResponseRequest object. We store this in response to access its fields later.
  2. Check Approval Status: We verify if response.accepted is True—this means the request was approved.
  3. Update the Linked RequestCar: Using response.request, we get the associated RequestCar instance (since request is the ForeignKey in ResponseRequest pointing to RequestCar). We then set its situation to "CONFIRMED" and save the changes.
  4. Optional Denied Handling: Added an optional section to set situation to "DENIED" if the request was rejected—feel free to remove this if you don't need it.

Important Notes:

  • Make sure your AnswerRequestForms (a ModelForm for ResponseRequest) includes the accepted field so users can select the approval status.
  • If your view is meant to reply to a specific RequestCar, you should modify the view to accept a request_id parameter. For example:
    def replyGeneralRequest(request, request_id, template_name="answer/reply_request.html"):
        target_request = RequestCar.objects.get(id=request_id)
        if request.method == 'POST':
            form = AnswerRequestForms(request.POST, instance=ResponseRequest(request=target_request))
            # ... rest of the validation logic ...
        else:
            form = AnswerRequestForms(initial={'request': target_request})
        # ... rest of the view code ...
    
    This ensures the ResponseRequest is automatically linked to the correct RequestCar without requiring the user to select it manually.

内容的提问来源于stack exchange,提问作者Aníbal Filho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:20:16