为Django应用程序创建REST API

The*_*Kid 28 python django rest

我得到了一项任务,我必须使用Django技术创建一个应用程序API(REST).我只需要能够从多个模型中读取(GET)条目,加入它们,并使用JSON格式(一个或多个对象)返回它们.json模式和适当的json文件的示例已经提供给我.

由于这是我第一次创建API而且我不熟悉Django,所以我很乐意向您提供一些指导.

我搜索了两个似乎最受欢迎的框架:

正如我所见,这两个使您能够快速为您的应用程序设置API.但是我可以使用其中一种创建自定义JSON格式,还是有另一种方法可以做到这一点?

Nul*_*ify 18

使用Tastypie: -

models.py

class User(Document):
    name = StringField()
Run Code Online (Sandbox Code Playgroud)

api.py

from tastypie import authorization
from tastypie_mongoengine import resources
from project.models import *
from tastypie.resources import *

class UserResource(resources.MongoEngineResource):
class Meta:
    queryset = User.objects.all()
    resource_name = 'user'
    allowed_methods = ('get', 'post', 'put', 'delete','patch')
    authorization = authorization.Authorization()
Run Code Online (Sandbox Code Playgroud)

url.py

from tastypie.api import Api
from projectname.api import *

v1_api = Api(api_name='v1')
v1_api.register(UserResource())
Run Code Online (Sandbox Code Playgroud)

Javascript(jQuery)

此示例是GET请求:

$(document).ready(function(){
    $.ajax({
       url: 'http://127.0.0.1:8000/api/v1/user/?format=json',
       type: 'GET',                   
       contentType: 'application/json',
       dataType: 'json',
       processData: false,
       success: function(data){
           alert(data)
       //here you will get the data from server
       },
       error: function(jqXHR, textStatus, errorThrown){
              alert("Some Error")                                  
       }
    })
})
Run Code Online (Sandbox Code Playgroud)

对于POST请求,请将类型更改为POSTdata以适当的格式发送

有关更多详细信息,请参阅Tastypie文档


Jav*_*ier 12

我使用过Django REST框架,总的来说就是它的工作方式.自动生成的人类可浏览的API屏幕也非常方便.

从理论上讲,它不强制要求任何表示形式; 定义"序列化程序",指定要公开的字段和内容,以及使用哪种串行格式.有些格式比其他格式更容易.最终,您可以添加简单的基于函数的视图,返回所需的确切JSON对象.即使在这种情况下,该框架也显着减少了获得完整API所需的工作量.

就像Django一样,最好的方法是至少完成整个教程一次,以了解到底发生了什么.在这样做时,不要屈服于将示例修改为特定问题的诱惑,它只会使事情变得更复杂.完成整个教程后,您可以告诉自己"简单"格式与您的需求有多接近.


jfu*_*unk 5

使用Django REST框架

Django 1.8.4DRF 3.3.3

这是一个非常简单的自定义JSONSchemaField类,您可以使用Django REST Framework和该jsonschema包(通过提供pip install jsonschema)来进行支持。

自定义字段从DRF的现有JSONField类继承而来,但有一些小的更改。它添加了根据JSONSchema定义验证传入JSON的步骤。如果验证通过,TextField则使用Django模型存储/检索原始JSON字符串。

在app / serializers.py中

import json
from rest_framework import serializers
from jsonschema import validate  # validates incoming data against JSONSchema
from jsonschema.exceptions import ValidationError as JSONSchemaValidationError
from .models import Lesson

from .jsonschema import (
    notes_schema,
)


class JSONSchemaField(serializers.JSONField):
# Custom field that validates incoming data against JSONSchema, 
# Then, if successful, will store it as a string.

    def __init__(self, schema, *args, **kwargs):
        super(JSONSchemaField, self).__init__(*args, **kwargs)
        self.schema = schema

    def to_representation(self, obj):
        return json.loads(obj)

    def to_internal_value(self, data):
        try:
            validate(data, self.schema)
        except JSONSchemaValidationError as e:
            raise serializers.ValidationError(e.message)

        return super(JSONSchemaField, self).to_internal_value(json.dumps(data))


class LessonSerializer(serializers.HyperlinkedModelSerializer):
    notes = JSONSchemaField(notes_schema)

    class Meta:
        model = Lesson
        fields = ('url', 'title', 'bpm', 'notes')
Run Code Online (Sandbox Code Playgroud)

在app / models.py中

from django.db import models


class Lesson(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='Untitled')
    bpm = models.DecimalField(max_digits=5, decimal_places=2, default=120.00)
    notes = models.TextField()

    class Meta:
        ordering = ('created',)
Run Code Online (Sandbox Code Playgroud)

在app / jsonschema.py中

notes_schema = {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "pattern": "^[A-G][#b]?[0-9]$"
            },
            "duration": {
                "type": "string",
                "pattern": "^\d+\/\d+$"
            }
        },
        "required": ["name", "duration"]
    }
}

notes_example = [{"name": "C#4", "duration": "1/4"},
                 {"name": "A4", "duration": "1/32"}]
Run Code Online (Sandbox Code Playgroud)

在app / views.py中

from rest_framework import viewsets

from .models import Lesson
from .serializers import LessonSerializer


class LessonViewSet(viewsets.ModelViewSet):
    queryset = Lesson.objects.all()
    serializer_class = LessonSerializer
Run Code Online (Sandbox Code Playgroud)