django 多对多序列化
-
serializer.py
# 序列化角色 class RoleSerializer(serializers.ModelSerializer): class Meta: model = models.Role fields = '__all__' # 序列化规则 class RuleSerializer(serializers.ModelSerializer): #查询所有规则信息,包括角色 roles = RoleSerializer(many=True, read_only=True) class Meta: model = models.Rule fields = '__all__'
- views.py
#查询所有规则对应的角色 class Rule_Role_View(APIView): def get(self,request): # 查询所有规则对应的角色 ruleobj = models.Rule.objects.all() guize = RuleSerializer(ruleobj, many=True) if guize: return Response({ 'status':200, 'msg':'', 'data':guize.data }) return Response({ 'status': 201, 'msg': '网络错误,请稍后重试', 'data': '' })
- postman获取测试
本文来自博客园,作者:手可摘星辰/*,转载请注明原文链接:https://www.cnblogs.com/u-damowang1/p/12159278.html