使用 djangorestframework 而不是 JsonResponse 有什么额外的优势吗?
Is there any added advantage of using djangorestframework over JsonResponse?
我是 Django 和 API 创作的新手。我想弄清楚使用 djangorestframework
or just use JsonResponse
. I got the suggestion of djangorestframework
from Digital Ocean's tutorial 是否更好,但也发现了 JsonResponse
,考虑到我不必安装另一个包,这似乎更简单。
目标:我希望能够为 Web 和移动应用程序提供用户信息。
我看到 post 为 djangorestframework
提供了一些原因,我将其粘贴在下面以供后验。
The common cases for using DRF are:
1)You're creating a public-facing external API for third-party
developers to access the data in your site, and you want to output
JSON they can use in their apps rather than HTML.
2)You're doing mobile development and you want your mobile app to make
GET/PUT/POST requests to a Django backend, and then have your backend
output data (usually as JSON) to the mobile app. Since you don't want
to pass back HTML to the mobile app, you use DRF to effectively create
a REST API that your mobile app can call.
3)You're creating a web app, but you don't want to use the Django
templating language. Instead you want to use the Django ORM but output
everything as JSON and have your frontend created by a JavaScript MVC
framework such as React, Backbone, AngularJS, etc. In those cases, you
can use DRF to output JSON that the JavaScript framework can process.
DRF 基本上为您提供了许多在原始 django 中没有的 API 功能。
例如:
Serializers:制作序列化程序的一种声明方式(django 风格,如声明模型),当您使用 JsonResponse
时,您必须在任何地方告诉要序列化的内容,使用您必须导入的序列化程序它并只是使用它,这个序列化器也可以 save/update 对象。还支持 ORM 源连接您的模型(想想用 JsonResponse 序列化具有嵌套关系的模型有多困难)。
Web 可浏览API,您可以看到所有可用的端点。
要安装和使用的第三方包:https://www.django-rest-framework.org/community/third-party-packages/#existing-third-party-packages。
我是 Django 和 API 创作的新手。我想弄清楚使用 djangorestframework
or just use JsonResponse
. I got the suggestion of djangorestframework
from Digital Ocean's tutorial 是否更好,但也发现了 JsonResponse
,考虑到我不必安装另一个包,这似乎更简单。
目标:我希望能够为 Web 和移动应用程序提供用户信息。
我看到 post 为 djangorestframework
提供了一些原因,我将其粘贴在下面以供后验。
The common cases for using DRF are:
1)You're creating a public-facing external API for third-party developers to access the data in your site, and you want to output JSON they can use in their apps rather than HTML.
2)You're doing mobile development and you want your mobile app to make GET/PUT/POST requests to a Django backend, and then have your backend output data (usually as JSON) to the mobile app. Since you don't want to pass back HTML to the mobile app, you use DRF to effectively create a REST API that your mobile app can call.
3)You're creating a web app, but you don't want to use the Django templating language. Instead you want to use the Django ORM but output everything as JSON and have your frontend created by a JavaScript MVC framework such as React, Backbone, AngularJS, etc. In those cases, you can use DRF to output JSON that the JavaScript framework can process.
DRF 基本上为您提供了许多在原始 django 中没有的 API 功能。
例如:
Serializers:制作序列化程序的一种声明方式(django 风格,如声明模型),当您使用
JsonResponse
时,您必须在任何地方告诉要序列化的内容,使用您必须导入的序列化程序它并只是使用它,这个序列化器也可以 save/update 对象。还支持 ORM 源连接您的模型(想想用 JsonResponse 序列化具有嵌套关系的模型有多困难)。Web 可浏览API,您可以看到所有可用的端点。
要安装和使用的第三方包:https://www.django-rest-framework.org/community/third-party-packages/#existing-third-party-packages。