1、通过继承CustomModelViewSet
来自定义,该类是重写ModelViewSet。会自动生成list:查询、create:新增、update:修改、retrieve:单例、destroy:删除 (CustomModelSerializer)是重写ModelSerializer
from utils.viewset import CustomModelViewSet
from utils.serializers import CustomModelSerializer
class AreaSerializer(CustomModelSerializer):
"""
地区-序列化器
"""
child_count = serializers.SerializerMethodField(read_only=True)
def get_child_count(self, instance: Area):
return Area.objects.filter(parent=instance).count()
class Meta:
model = Area
fields = "__all__"
read_only_fields = ["id"]
class AreaCreateUpdateSerializer(CustomModelSerializer):
"""
地区管理 创建/更新时的列化器
"""
class Meta:
model = Area
fields = '__all__'
class AreaViewSet(CustomModelViewSet):
"""
地区管理接口:
list:查询
create:新增
update:修改
retrieve:单例
destroy:删除
"""
queryset = Area.objects.all().order_by('id')
serializer_class = AreaSerializer
filter_fields = ['status','id','parent']
search_fields = ('name',)
def area_root(self, request):
queryset = self.filter_queryset(self.get_queryset())
queryset = queryset.filter(parent__isnull=True).order_by('id')
serializer = AreaSerializer(queryset, many=True)
return SuccessResponse(data=serializer.data, msg="获取成功")