django - Django 存在查询在 if 和 else 子句中不起作用?

在下面给出的情况下,如果 Medicine table 包含医学名称,则查询执行正常,但是当对应的 table 上不存在药物名称时,则会发生错误。

匹配的查询不存在

views.py 代码

@api_view(['POST'])
    def addPeople(request):
    m = People()
    m.bp_no = request.POST['bp_no']
    m.name = request.POST['name']
    m.corporation_name = request.POST['corporation_name']
    m.medicine_name = request.POST['medicine_name']
    m.no_of_medicine = request.POST['no_of_medicine']

    existing = Medicine.objects.get(medicine_name=m.medicine_name).no_of_medicine -  int(m.no_of_medicine)
    p_key = Medicine.objects.get(medicine_name=m.medicine_name).id
    if Medicine.objects.filter(medicine_name=m.medicine_name).exists():
      if existing > 0:
      m.save()       
      Medicine.objects.filter(id=p_key).update(no_of_medicine=existing)
      return Response({"message": "Successfully Recorded"})
      else:
       return Response({"message": "Not much Medicine Stored"})
    else:
       return Response({"message": "Medicine is not Stored"})

模型.py

class People(models.Model):
    bp_no = models.IntegerField(blank=False,null=False)
    name = models.CharField(blank=False,null=False,max_length=200)
    corporation_name = models.CharField(blank=False,null=False,max_length=200)
    medicine_name = models.CharField(blank=False,null=False,max_length=200)
    no_of_medicine = models.IntegerField()

class Medicine(models.Model):
    medicine_name = models.CharField(null=False,blank=False,max_length=200)
    no_of_medicine = models.IntegerField(null=False,blank=False)

    def __str__(self):
        return self.medicine_name

错误回溯:当 Medicine table 不包含相应的过滤器名称时,将显示此错误

Internal Server Error: /api/add-people/
Traceback (most recent call last):
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\rest_framework\decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "E:\django\backend\medirecords\api\views.py", line 37, in addPeople
    existing = Medicine.objects.get(medicine_name=m.medicine_name).no_of_medicine -  int(m.no_of_medicine)       
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\MonirHossain\AppData\Roaming\Python\Python39\site-packages\django\db\models\query.py", line 435, in get
    raise self.model.DoesNotExist(
api.models.Medicine.DoesNotExist: Medicine matching query does not exist.

回答1

我没有对此进行测试,但纯粹就处理未找到的对象而言,我有一个建议您可以尝试简化条件/流程。

.get(): 包裹在 try / except

https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.query.QuerySet.get 在未找到时引发 https://docs.djangoproject.com/en/4.0/ref/models/class/#django.db.models.Model.DoesNotExist

当没有找到时,您需要确定要如何处理它。例如,您可以使用默认后备 value,或引发错误。

在这种情况下,如果找不到现有的药物,代码将失败,所以我会提高它。

@api_view(["POST"])
def addPeople(request):
    medicine_name = request.POST["medicine_name"]
    try:
        medicine = Medicine.objects.get(medicine_name=medicine_name)
    except Medicine.DoesNotExist:
        return Response({"message": "Not much Medicine Stored"})
    m = People()
    m.bp_no = request.POST["bp_no"]
    m.name = request.POST["name"]
    m.corporation_name = request.POST["corporation_name"]
    m.medicine_name = medicine_name
    m.no_of_medicine = request.POST["no_of_medicine"]

    existing = medicine.no_of_medicine - int(m.no_of_medicine)

    p_key = medicine.id
    if medicine.exists():
        if existing > 0:
            m.save()
            medicine.num_of_medicine = existing
            medicine.save(update_fields=["num_of_medicine"])
            return Response({"message": "Successfully Recorded"})
        else:
            return Response({"message": "Not much Medicine Stored"})
    else:
        return Response({"message": "Medicine is not Stored"})

get_object_or_404()

https://docs.djangoproject.com/en/4.0/topics/http/shortcuts/#get-object-or-404 也可以用来代替前面的 try/catch 块:

from django.shortcuts import get_object_or_404

medicine = get_object_or_404(Medicine.objects.all(), medicine_name=m.medicine_name)
existing = medicine.no_of_medicine - int(m.no_of_medicine)

最后说明

我假设这是一个示例,但在 POST 数据中验证 request.POST["no_of_medicine"] 以确保它不是伪造的后端数据源也可能很好。

相似文章

随机推荐

最新文章