django-rest-framework:处理业务逻辑以及返回什么? [英] django-rest-framework: Handling business logic and what to return?

查看:65
本文介绍了django-rest-framework:处理业务逻辑以及返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的API中,一个人可以提交1个孩子的父母".这是常见的用例.输入父母时,您始终输入至少一个孩子.在用户界面上也是如此.在某些情况下,用户可能想要输入重复的父母,例如.它已经存在于系统中.在这种情况下,在UI中,用户可以选择是否确实要添加重复项,或者是否要将子级添加到现有重复"记录之一中.我希望这足够清楚.

In my API a one can submit "Parent" with 1 child. This is the common use case. You always enter at least one child when entering parent. This is the same on the UI. There can be the case where the user will want to enter a duplicate parent, eg. it already exists in the system. In that case, in the UI, the user gets to choose if he indeed wants to add a duplicate or if he wants to add the child to one of the existing "duplicate" records. I hope this was clear enough.

我的问题是如何通过api进行类似的工作流程"?提交副本后,它应返回可以添加子项的可能现有条目的列表.该清单应如何退还?具有什么状态码?(实际上不是客户端错误)

My question is how I can do a similar "workflow" via the api? Upon submission of duplicate, it should return a list of possible existing entries to which child can be added. How should this list be returned? With what status code? (not really a client error)

推荐答案

所以您的请求类似于:

POST /parents

{"name": "Foo", "child": { ... }}

如果"Foo"已经存在,您需要客户端选择一个现有记录并在其中添加子项,对吗?然后,像这样的响应可能是适当的:

And if "Foo" already exists, you require the client to choose an existing record and add the child there, correct? Then a response like this may be appropriate:

HTTP/1.1 409 Conflict

{
  "message": "Choose an existing parent.",
  "parents": [
    {"id": 1, ...},
    {"id": 2, ...},
    {"id": 42, ...}
  ]
}

这需要客户端将请求重复到类似以下内容:

This requires the client to repeat the request to something like:

POST /parents/42/children

{ ... }

这篇关于django-rest-framework:处理业务逻辑以及返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆