使用DRF开发API
This commit is contained in:
0
myapp_api/__init__.py
Normal file
0
myapp_api/__init__.py
Normal file
3
myapp_api/admin.py
Normal file
3
myapp_api/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
myapp_api/apps.py
Normal file
6
myapp_api/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MyappApiConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'myapp_api'
|
||||
24
myapp_api/migrations/0001_initial.py
Normal file
24
myapp_api/migrations/0001_initial.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# Generated by Django 5.2.8 on 2025-11-15 03:44
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='User',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=100)),
|
||||
('city', models.CharField(max_length=100)),
|
||||
('sex', models.CharField(max_length=100)),
|
||||
('age', models.IntegerField()),
|
||||
],
|
||||
),
|
||||
]
|
||||
0
myapp_api/migrations/__init__.py
Normal file
0
myapp_api/migrations/__init__.py
Normal file
8
myapp_api/models.py
Normal file
8
myapp_api/models.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
class User(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
city = models.CharField(max_length=100)
|
||||
sex = models.CharField(max_length=100)
|
||||
age = models.IntegerField()
|
||||
3
myapp_api/tests.py
Normal file
3
myapp_api/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
7
myapp_api/views.py
Normal file
7
myapp_api/views.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from rest_framework import viewsets
|
||||
from .serializers import UserSerializer
|
||||
from myapp_api.models import User
|
||||
|
||||
class UserViewSet(viewsets.ModelViewSet):
|
||||
queryset = User.objects.all() # 指定操作的数据
|
||||
serializer_class = UserSerializer # 指定序列化器
|
||||
Reference in New Issue
Block a user