使用DRF开发API

This commit is contained in:
2025-11-15 12:12:09 +08:00
parent 3a0e3e9ad1
commit fe052af0f1
9 changed files with 63 additions and 0 deletions

12
.idea/material_theme_project_new.xml generated Normal file
View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MaterialThemeProjectNewConfig">
<option name="metadata">
<MTProjectMetadataState>
<option name="migrated" value="true" />
<option name="pristineConfig" value="false" />
<option name="userId" value="-2b504815:19a7605d86e:-7ffe" />
</MTProjectMetadataState>
</option>
</component>
</project>

0
myapp_api/__init__.py Normal file
View File

3
myapp_api/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
myapp_api/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class MyappApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myapp_api'

View 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()),
],
),
]

View File

8
myapp_api/models.py Normal file
View 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
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

7
myapp_api/views.py Normal file
View 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 # 指定序列化器