使用 Django REST 框架
与Django 1.8.4
和DRF 3.3.3
。
这是一个非常简单的自定义 JSONSchemaField 类,您可以使用 Django REST Framework 和jsonschema
包(可通过 获得pip install jsonschema
)来支持。
自定义字段继承自 DRF 的现有JSONField
类,并进行了一些小的更改。它添加了根据 JSONSchema 定义验证传入 JSON 的步骤。如果验证通过,则使用 Django 模型TextField
存储/检索原始 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')
在 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',)
在 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"}]
在应用程序/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