django-rest-framework - 可浏览API中的自动生成表单? [英] django-rest-framework - autogenerate form in browsable API?

查看:190
本文介绍了django-rest-framework - 可浏览API中的自动生成表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道我是否使用正确的词汇。在django-rest-framework免费的可浏览api中,我想知道是否有一种自动生成与我们定义ModelForms类似的表单的方法。在某些情况下,这将允许我们更容易地测试对API的输入。
我正在使用ModelSerializers和通用视图APIView,以防发生变化。

Not sure if i'm using the right vocabulary. In the browsable api that comes for free with django-rest-framework, I was wondering if there was a way to autogenerate a form similar to how we define ModelForms. This would allow us to more easily test input to the API in some cases. I'm currently using ModelSerializers and the generic view APIView in case that makes a difference.

我已经阅读了文档(此时已经有几次),但是没有看到它在任何地方提到。

I have read the documentation (several times at this point) but didn't see it mentioned anywhere.

推荐答案

如果您使用通用的基于类的视图,那么您将免费获得。尝试使用 http://restframework.herokuapp.com 的实时教程,作为其中一个用户登录,以便您可以创建一些片段。例如user:'max',password:'max'。

If you're using the generic class-based-views you'll get that for free. Try the live tutorial at http://restframework.herokuapp.com logging in as one of the users, so that you can create some snippets. eg user: 'max', password: 'max'.

任何视图子类化 GenericAPIView 并设置 serializer_class 将会得到这个行为,因为REST框架可以确定这个表单应该是什么样子。

Any views subclassing GenericAPIView and setting a serializer_class will get that behavior, as REST framework can determine what the form should look like.

例如:

(注意屏幕截图底部的表单输入)

(Note the form input at the bottom of the screen shot)

如果您刚从 APIView 你会得到一般的内容输入(如json),就像一旦你添加了一个屏幕截图,这也是有用的,但不太方便的表单。

If you're just working from APIView you'll get the generic content input (such as json), like the once you've included a screenshot of, which is also useful, but not quite as convenient as the forms.

这篇关于django-rest-framework - 可浏览API中的自动生成表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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