使用DRF开发API
This commit is contained in:
7
myapp_api/serializers.py
Normal file
7
myapp_api/serializers.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from myapp_api.models import User
|
||||
from rest_framework import serializers
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User # 指定数据模型
|
||||
fields = '__all__' # 显示所有字段
|
||||
12
myapp_api/urls.py
Normal file
12
myapp_api/urls.py
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
from django.urls import path,include
|
||||
from myapp_api import views
|
||||
from rest_framework import routers
|
||||
|
||||
# 自动注册路由(url)
|
||||
router = routers.DefaultRouter()
|
||||
router.register(r'user', views.UserViewSet)
|
||||
|
||||
urlpatterns = [
|
||||
path('api/', include(router.urls)),
|
||||
]
|
||||
Reference in New Issue
Block a user