使用DRF开发API

This commit is contained in:
2025-11-15 12:11:31 +08:00
parent 7010faee4f
commit 3a0e3e9ad1
5 changed files with 25 additions and 2 deletions

7
myapp_api/serializers.py Normal file
View 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
View 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)),
]