DRF学习第一课
1.安装drf
pip3 install djangorestframework -i https://pypi.douban.com/simple
2.在settings.py的INSTALLED_APPS中添加’rest_framework’。
INSTALLED_APPS = [
...
'rest_framework',
]
3,看完后继承的类,写的代码越少,当然灵活度也越低,根据实际情况使用,会记录几种方法的使用。
from rest_framework import serializers
#序列化的类
#第一种,原始继承
class UserSerializer(serializers.Serializer):
user=serializers.CharField()
password=serializers.CharField()
#第二种,继承ModelSerializer,相对简单
class UserSerializer(serializers.ModelSerializer):
class Meta:
model=User
fields='__all__'
4,增删改查的继承
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
class UserView(APIView):
#所有数据
class UserView(GenericAPIView):
queryset=User.objects
serializer_class=UserSerializer
def get(self,request):
#第一种:serializer=self.serializer_class(instance=self.queryset,many=True)
#第二种:serializer=self.get_serializer_class()(instance=self.queryset,many=True)
#第三种:
#serializer=self.get_serializer(instance=self.queryset,many=True)
serializer=self.get_serializer(instance=self.get_queryset(),many=True)
return Response(serializer.data)
#单个数据
class UserDetailView(GenericAPIView):
queryset=User.objects
serializer_class=UserSerializer
def get(self,request,pk):
serializer=self.get_serializer(instance=self.get_object(),many=True)
return Response(serializer.data)
def put(self,request,pk):
serializer=self.get_serializer(instance=self.get_object(),data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
else:
return Response(serializer.errors)
def delete(self,request,pk):
self.get_object().delete()
return Response()
mixins混合类:
from rest_framework.mixins import CreateModelMixin,ListModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
#所有数据
class UserView(GenericAPIView,ListModelMixin,CreateModelMixin):
queryset=User.objects
serializer_class=UserSerializer
def get(self,request):
return self.list(request)
def post(self,request):
return self.create(request)
#单个数据
class UserDetailView(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
queryset=User.objects
serializer_class=UserSerializer
def get(self,request,pk):
return self.retrieve(request,pk)
def put(self,request,pk):
return self.update(request,pk)
def delete(self,request,pk):
return self.destroy(request,pk)
在继承更简单的封装类,上面的DRF已经封装在ListCreateAPIView,RetrieveUpdateDestroyAPIView中了
from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView
#所有数据
class UserView(ListCreateAPIView):
queryset=User.objects
serializer_class=UserSerializer
#单个数据
class UserDetailView(RetrieveUpdateDestroyAPIView):
queryset=User.objects
serializer_class=UserSerializer
继续优化,使用更简单的类。
#urls.py路由中
path('user',UserView.as_view({"get":"list","post":"create"})),
re_path('user/(?P<pk>\d+)',UserView.as_view({"get":"retrieve","put":"update","delete","detroy"})),
from rest_framework.viewsets import ViewSetMixin
class UserSerializer(serializers.ModelSerializer):
class Meta:
model=User
fields='__all__'
class UserView(ViewSetMixin,APIView):
def list(self,request):
return
def single(self,request):
return
def post(self,request):
return
def edit(self,request):
return
最终版本优化,三行代码搞定:
from rest_framework.viewsets import ModelViewSet
class UserSerializer(serializers.ModelSerializer):
class Meta:
model=User
fields='__all__'
def validate_user(self,value):
if value.endswith("py"):
return value
else:
raise serializers.ValidationError("用户名名称结尾必须以py结尾!")
class UserView(ModelViewSet):
queryset=User.objects
serializer_class=UserSerializer
额,大概就这样,基础学完了
本文来自博客园,作者:super_ip,转载请注明原文链接:https://www.cnblogs.com/superip/p/17402169.html