标签:
操作Django实现RESTful APIRESTful API此刻很风行,这里是它的介绍 理解RESTful架谈判 RESTful API设计指南.凭据Django的通例要领固然也可以实现REST,但有一种更快捷、强大的要领,那就是 Django REST framework.它是python的一个模块,通过在Django里面配置就可以把app的models中的各个表实现RESTful API。下面是实现要领:
一、安置配置pip install djangorestframework pip install markdown # Markdown support for the browsable API.pip install django-filter # Filtering support再到Django的 settings.py 中的INSTALLED_APPS添加 rest_framework,如下:
1
2
3
4
INSTALLED_APPS = (
...
'rest_framework',
)
在根目录的 url.py 文件中为rest_framework框架的 login 和 logout 视图添加url:
1
2
3
4
urlpatterns = [
...
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
二、创建model和Serializer
创建app,名为 snippets.。在视图 models.py 中添加一张表如下:
from django.db import modelsfrom 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',)然后开始同步到数据库中:
./manage.py makemigrations snippets ./manage.py migrate接下来需要做的就是创建 Serializer 类,类似于 Form。它的感化就是从你传入的参数中提取出你需要的数据,并把它转化为 json 格局(注意,已经是字节码了),同时撑持反序列化到model东西。在 snippets 文件夹中添加 serializers.py 并在其添加如下:
from rest_framework import serializersfrom snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES class SnippetSerializer(serializers.Serializer): # 它序列化的方法很类似于Django的forms 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'}) # style的设置等同于Django的 widget=widgets.Textarea 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三、使用Serializer先使用 ./manage.py shell 进入Django的shell中。操纵如下: