path('get2/', views.HelloApiView().get), path('get2/', views.HelloApiView.as_view())
models
class Student(models.Model):name = models.CharField(max_length=100,verbose_name='姓名')age = models.IntegerField(verbose_name='年龄')sex = models.BooleanField(default=1,verbose_name='性别')class_null = models.CharField(max_length=5,verbose_name='班级编号')class Meta:db_table = 'tb_student'
python manage.py makemigrations myapp
生成表
python manage.py migrate
数据库
INSTALLED_APPS 中添加app名称
执行之后的结果
class Book(models.Model):title = models.CharField(max_length=32,verbose_name='书籍')price = models.IntegerField(verbose_name='价格')pub_date = models.DateField(verbose_name='出版日期')bread = models.IntegerField(verbose_name='阅读量')bcomment = models.IntegerField(verbose_name='评论量')publish = models.ForeignKey('Publish',on_delete=models.CASCADE,verbose_name='出版社')def __str__(self):return self.title
class Publish(models.Model):name = models.CharField(max_length=32,verbose_name='出版社名称')email = models.EmailField(verbose_name='出版社邮箱')def __str__(self):return self.name
class Author(models.Model):name = models.CharField(max_length=32,verbose_name='作者')age = models.IntegerField(verbose_name='年龄')def __str__(self):return self.name
三个表
serializers 里面包含哪些字段
from rest_framework import serializers
class StudentSerializer(serializers.Serializer):name = serializers.CharField()
序列化
return Response(serializer.data)
返回的值就会是
students = Student.objects.all()
names = serializers.CharField(source='name')
names的赋值
re_path('get3/(\d+)/', views.Details.as_view())
def get(self,request,id=None):students = Student.objects.get(pk=id)serializer = StudentSerializer(instance=students, many=False)
反序列化