环境ubuntu 16.04 已经安装了 py2 和py3
目录:
cd /home/jack/django_rest/blog
python3 -m venv env
source env/bin/activate
python -V
Python 3.5.2
更新pip 版本
python -m pip install --upgrade pip
pip install django
pip install djangorestframework
pip install django-filter
pip list
Package Version
------------------- -------
Django 2.2.6
django-filter 2.2.0
djangorestframework 3.10.3
pip 19.3.1
pkg-resources 0.0.0
pytz 2019.3
setuptools 20.7.0
sqlparse 0.3.0
django-admin startproject mblog
cd mblog/
python manage.py startapp mainsite
vi mainsite/models.py
from django.db import models
class User(models.Model):
name = models.CharField(max_length=32)
mail = models.EmailField()
class Entry(models.Model):
STATUS_DRAFT = "draft"
STATUS_PUBLIC = "public"
STATUS_SET = (
(STATUS_DRAFT, "草稿"),
(STATUS_PUBLIC, "公开"),
)
title = models.CharField(max_length=128)
body = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
status = models.CharField(choices=STATUS_SET, default=STATUS_DRAFT, max_length=8)
author = models.ForeignKey(User, related_name='entries', on_delete=models.CASCADE)
vi mblog/settings.py
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainsite',
]
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
admin
密码:123abc
python manage.py runserver 192.168.99.218:8080
http://192.168.99.218:8080/
http://192.168.99.218:8080/admin/
想要显示刚才添加的两个Model
vi mainsite/admin.py
from django.contrib import admin
from .models import User, Entry
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
pass
@admin.register(Entry)
class Entry(admin.ModelAdmin):
pass
vi mblog/settings.py
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainsite',
'rest_framework'
]
vi mainsite/serializer.py
# coding: utf-8
from rest_framework import serializers
from .models import User, Entry
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('name', 'mail')
class EntrySerializer(serializers.ModelSerializer):
class Meta:
model = Entry
fields = ('title', 'body', 'created_at', 'status', 'author')
vi mainsite/views.py
from django.shortcuts import render
# Create your views here.
# coding: utf-8
import django_filters
from rest_framework import viewsets, filters
from .models import User, Entry
from .serializer import UserSerializer, EntrySerializer
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class EntryViewSet(viewsets.ModelViewSet):
queryset = Entry.objects.all()
serializer_class = EntrySerializer
vi mblog/urls.py
# coding: utf-8
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from mainsite.urls import router as mainsite_router
urlpatterns = [
path('admin/', admin.site.urls),
#include blog.urls
url(r'^api/', include(mainsite_router.urls)),
]
vi mainsite/urls.py
# coding: utf-8
from rest_framework import routers
from .views import UserViewSet, EntryViewSet
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'entries', EntryViewSet)
python manage.py runserver 192.168.99.218:8080
http://192.168.99.218:8080/api/
http://192.168.99.218:8080/api/users/
http://192.168.99.218:8080/admin/
添加Users
执行如下的连接:
http://192.168.99.218:8080/api/users/
在以上图片的post处,添加Name 和Mail
然后点击POST
再次 执行 如下连接:
http://192.168.99.218:8080/api/users/
修改一下文件:
vi mainsite/serializer.py
class EntrySerializer(serializers.ModelSerializer):
class Meta:
model = Entry
fields = ('title', 'body', 'created_at', 'status', 'author')
修改为:
class EntrySerializer(serializers.ModelSerializer):
# author的serializer
author = UserSerializer()
class Meta:
model = Entry
fields = ('title', 'body', 'created_at', 'status', 'author')
vi mblog/settings.py
ALLOWED_HOSTS = ['*']
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 2
}
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainsite',
'rest_framework'
]
效果如下:
http://192.168.99.218:8080/api/users/
vi mblog/settings.py
ALLOWED_HOSTS = ['*']
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 2,
'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',),
}
# Application definition
vi mainsite/views.py
原来的:
class EntryViewSet(viewsets.ModelViewSet):
queryset = Entry.objects.all()
serializer_class = EntrySerializer
修改后的:
class EntryViewSet(viewsets.ModelViewSet):
queryset = Entry.objects.all()
serializer_class = EntrySerializer
filter_fields = ('author', 'status')
Django笔记 Django REST Framework实现Web API 1
https://blog.csdn.net/SVALBARDKSY/article/details/50548073
RESTful API 设计指南
http://www.ruanyifeng.com/blog/2014/05/restful_api.html
理解RESTful架构
http://www.ruanyifeng.com/blog/2011/09/restful.html