Django 之REST framework学习1:Serialization(序列化) - Go语言中文社区

Django 之REST framework学习1:Serialization(序列化)


本文章示例均来自官方文档,如有不明确的地方,可以参考官方文档:

http://www.django-rest-framework.org/tutorial/1-serialization/

强插图一张:
hello,REST framework

Let’s roll:

1、我们建立一个自己的项目,官方用的都是命令,我们还是用PyCharm吧,简单粗暴!:

项目结构

2、在

INSTALLED_APPS = (
    ...
    'rest_framework',
    #这一条我们的神器PyCharm会给我们自动加上,么么哒~
    #'snippets.apps.SnippetsConfig',
)
Okay, we’re ready to roll.

3、在

from django.db import models
#这两个imports是在控制台代码显示高亮用的,可以无视
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles

LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())


class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=False)
    language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
    style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)

    class Meta:
        ordering = ('created',)

4、model类创建好后做数据库迁移:

python manage.py makemigrations snippets
python manage.py migrate

5、在snippets 目录下创建 serializers.py并在 serializers.py创建一个可序列化的SnippetSerializer类,这个类和Django的forms非常像,所以说这个类的写法和用法就和forms特别像:

from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES


class SnippetSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')

    def create(self, validated_data):
        """
        Create and return a new `Snippet` instance, given the validated data.
        """
        return Snippet.objects.create(**validated_data)

    def update(self, instance, validated_data):
        """
        Update and return an existing `Snippet` instance, given the validated data.
        """
        instance.title = validated_data.get('title', instance.title)
        instance.code = validated_data.get('code', instance.code)
        instance.linenos = validated_data.get('linenos', instance.linenos)
        instance.language = validated_data.get('language', instance.language)
        instance.style = validated_data.get('style', instance.style)
        instance.save()
        return instance

PS:当调用serializer.save()时,
这个类的create()和update(),
控制了完整实例的创建或者修改!!
从这个类的定义中看到,这个类的写法和form的写法很像:
连字段都一样:required,max_length ,default
{‘base_template’: ‘textarea.html’}和form的widget=widgets.Textarea一样,

上面那么写是不是很复杂,当然REST framework提供了更简单的类ModelSerializer:
#这个类就简化了好多,里面也包含了create和update方法:
class SnippetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Snippet
        fields = ('id', 'title', 'code', 'linenos', 'language', 'style')

我们用这个类的时候,需要:
实例化Snippet类:
snippet = Snippet(code=’foo = “bar”n’)
snippet.save()

snippet = Snippet(code=’print “hello, world”n’)
snippet.save()
进而序列化model类
serializer = SnippetSerializer(snippet)
.data就能取到snippet中的所有数据,也是将数据组织为字典格式:
serializer.data
{‘id’: 2, ‘title’: u”, ‘code’: u’print “hello, world”n’, ‘linenos’: False, ‘language’: u’python’, ‘style’: u’friendly’}

序列化:

from rest_framework.renderers import JSONRenderer
#序列化调用:
content = JSONRenderer().render(serializer.data)
#这时的content:
#'{"id": 2, "title": "", "code": "print \"hello, world\"\n", "linenos": false, "language": "python", "style": "friendly"}'

反序列化:

from rest_framework.parsers import JSONParser
from django.utils.six import BytesIO

stream = BytesIO(content)
data = JSONParser().parse(stream)
获取到data后我们可以将字典数据放入OrderedDict对象中:
serializer = SnippetSerializer(data=data)
serializer.is_valid()
# True
serializer.validated_data
# OrderedDict([('title', ''), ('code', 'print "hello, world"n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])
serializer.save()
# <Snippet: Snippet object>
除了序列化model对象我们也可以序列化queryset类型数据但是注意要加入参数many=True:

serializer = SnippetSerializer(Snippet.objects.all(), many=True)
serializer.data

6、现在在

from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
#因为要发送post请求,所以不能使用csrftoken,exempt是豁免的意思,加上装饰器:
@csrf_exempt
def snippet_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)

@csrf_exempt
def snippet_detail(request, pk):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        snippet = Snippet.objects.get(pk=pk)
    except Snippet.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = SnippetSerializer(snippet)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(snippet, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        snippet.delete()
        return HttpResponse(status=204)

7、

from django.conf.urls import url
from snippets import views

urlpatterns = [
    url(r'^snippets/$', views.snippet_list),
    url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail),
]

8、

from django.conf.urls import url, include

urlpatterns = [
    url(r'^', include('snippets.urls')),
]

9、测试:

result

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_33733970/article/details/79113235
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-06-14 00:08:05
  • 阅读 ( 997 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢