django简化序列化器及视图.路由编写

django简化序列化器及视图.路由编写,第1张

1.编写models模型类
from django.db import models

# Create your models here.
class Province(models.Model):
    pro_name = models.CharField(max_length=20,verbose_name='省份名称')
    class Meta:
        verbose_name = '省份表'
        verbose_name_plural = verbose_name
        db_table = 'Province'
    def __str__(self):
        return self.pro_name

class Student(models.Model):
    name = models.CharField(max_length=20,verbose_name='学生')
    score = models.IntegerField(verbose_name='成绩')
    sex = models.CharField(max_length=20,verbose_name='性别')
    province = models.ForeignKey(to=Province,on_delete=models.CASCADE,verbose_name='省份名称')
    class Meta:
        verbose_name = '学生表'
        verbose_name_plural = verbose_name
        db_table = 'Student'
    def __str__(self):
        return self.name
2.pycharm中的子应用目录下创建序列化器python文件 serializers
from rest_framework import serializers   #导入serializers
from Province.models import Province,Student    #导入模型类

# 编写序列化器 继承的是serializers中的ModelSerializer
class ProvinceSerializers(serializers.ModelSerializer):
    class Meta:
        #  model中Province模型
        model = Province
        #  字段映射'__all__'所有字段
        fields = '__all__'

class StudentSerializers(serializers.ModelSerializer):
    #  增加pro_name字段 用于读取省份名称
    #  自定义字段pro_name 继承serializers中的SerializerMethodField
    #  (read_only=True) 只读
    pro_name = serializers.SerializerMethodField(read_only=True)
    #  def定义pro_name  用get获取的方法  obj指具体的某一个对象
    def get_pro_name(self,obj):
        #  返回 obj对象中调用外键province中的pro_name字段 (省份表中pro_name省份名称)
        return obj.province.pro_name

    class Meta:
        model = Student
        fields = '__all__'
3.编写views视图
from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet  #导入ModelViewSet
from rest_framework.filters import SearchFilter,OrderingFilter   #导入过滤和排序
from Province.models import Province,Student    #导入模型类
from Province.serializers import ProvinceSerializers,StudentSerializers  #导入序列化器

# Create your views here.

# 继承ModelViewSet 实现对省份的增删查改
class ProvinceView(ModelViewSet):
    queryset = Province.objects.all()
    serializer_class = ProvinceSerializers

class StudentView(ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializers

    # 导入过滤,排序功能
    filter_backends = [SearchFilter,OrderingFilter]
    # 过滤字段 通过过滤出的字段来查询
    search_fields = ['name','province__id']
    # 排序字段
    ordering_fields = ['score']
4.编写路由
from django.urls import path
from rest_framework import routers  #导入routers
from Province import views  # 导入views视图
urlpatterns = [

]
# 用routers生成路由对象 通过routers中的SimpleRouter()
router = routers.SimpleRouter()
# 用router.register生成路由信息
# 'province' 前面是路由,后面是给路由起的名字,中间调用views中ProvinceView视图
router.register('province',views.ProvinceView,'province')
router.register('student',views.StudentView,'student')
# 路由追加,上面urlpatterns += router.urls
urlpatterns += router.urls

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/918123.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存