首页 > 代码库 > Django-rest-framework 的使用

Django-rest-framework 的使用

安装:

 

Install using pip, including any optional packages you want...pip install djangorestframeworkpip install markdown       # Markdown support for the browsable API.pip install django-filter  # Filtering support

 

获取代码

 

...or clone the project from github.git clone git@github.com:tomchristie/django-rest-framework.git

 

setting 配置

INSTALLED_APPS = (    ...    rest_framework,)


REST_FRAMEWORK = {    # Use Django‘s standard `django.contrib.auth` permissions,    # or allow read-only access for unauthenticated users.    ‘DEFAULT_PERMISSION_CLASSES‘: [        ‘rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly‘    ]}

If youre intending to use the browsable API youll probably also want to add REST frameworks login and logout views. Add the following to your root urls.py file.urlpatterns = [ ... url(r^api-auth/, include(rest_framework.urls, namespace=rest_framework))]

Example

url配置


from rest_framework import routers
from crm.rest_views import UserViewSet,RoleViewSet
# Routers provide an easy way of automatically determining the URL conf.
  router = routers.DefaultRouter()
  router.register(r‘users‘, UserViewSet)
  router.register(r‘roles‘, RoleViewSet)

# Wire up our API using automatic URL routing.# Additionally, we include login URLs for the browsable API.urlpatterns = [    url(r‘^api/‘, include(router.urls)),
 url(r‘^apitest/‘, views.api_test  ),
url(r^api-auth/, include(rest_framework.urls, namespace=rest_framework))]
rest_serializer配置
数据序列化

rom crm import models
from rest_framework import serializers

# Serializers define the API representation.
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = models.UserProfile
depth = 3
fields = (‘url‘, ‘email‘, ‘name‘, ‘is_staff‘,‘is_active‘,‘role‘)


class RoleSerializer(serializers.ModelSerializer):
class Meta:
model = models.Role
fields = ("name",)

如果是外检 需要导入表
rest_views配置
from crm import modelsfrom crm.rest_serializer import UserSerializer,RoleSerializerfrom rest_framework import viewsetsclass UserViewSet(viewsets.ModelViewSet): queryset = models.UserProfile.objects.all() # 取到数据 serializer_class = UserSerializer# 序列化class RoleViewSet(viewsets.ModelViewSet): queryset = models.Role.objects.all() serializer_class = RoleSerializer

views 视图
def api_views(request):
if request.method=="POST":
data = http://www.mamicode.com/json.loads(request.POST.get(‘data‘))
print(data)
serialize_obj=rest_serializer.UserSerializer(data=http://www.mamicode.com/data)
if serialize_obj.is_valid():
serialize_obj.save()

return render(request,‘crm/api-test.html‘,locals())

 

api_test.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <form method="POST"> {% csrf_token %} <textarea name="data" cols="88" rows="6"></textarea> <input type="submit" > </form>{{ serialize_obj }}</body></html>

初步研究 

 

 

官网文档  http://www.django-rest-framework.org/

 

 

 

Django-rest-framework 的使用