1.request
1. request.data
post请求内的数据都放在了request.data

2.request.query_parme
get 请求内携带的参数都放在了request.query_parms

3. request.FILES
def FILES(self):
# Leave this one alone for backwards compat with Django's request.FILES
# Different from the other two cases, which are not valid property
# names on the WSGIRequest class.
if not _hasattr(self, '_files'):
self._load_data_and_files()
return self._files
4.APIView分析
4.1 as_view 方法分析
4.2 dispatch 方法分析
4.3 initialize_request 方法分析
4,4 initial 方法分析
2. 路由
DRF之路由
在urls.py 配置路由

1.正常的
路由分发
import user
import home
urlpatterns = [
path('admin/', admin.site.urls),
path('user/',include('user.urls')),
path('home/',include('home.urls')),
path('course/',include('course.urls')),
# meida路由

2.继承了viewMixin
用过滤器怎么写呢。

3.继承了ModelViewSet
from rest_framework.routers import DefaultRouter
from rest_framework.routers import DefaultRouter,SimpleRouter
bookrouter=DefaultRouter()
bookrouter.register('books',views.BookModelViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns+=bookrouter.urls
   如果不想用pk 更改GenericAPIView的这个属性 lookup_field = 'pk'
action的使用
action的使用
为了给继承自modelViewset的视图类中定义的函数也添加路由
@action(methods=['get','POST'],detail=False)
# method
# true 表示生成带pk的地址
@action(methods=['get'],detail=False)
生成的路由
<URLPattern '^books/get_1/$' [name='book-get-1']>

@action(methods=['get'],detail=True)
<URLPattern '^books/(?P<pk>[^/.]+)/get_1/$' [name='book-get-1']>
http://127.0.0.1:8000/books/pk=1/get_1/
 视图类的写法
@action(methods=['get','post'],detail=True)
    def get_1(self,request,*args,**kwargs):
        print('get')
        book=self.get_queryset()[:2]
        ser=self.get_serializer(book,many=True)

 

 
  
 
posted on 2023-04-20 14:22  阿勒泰的鱼  阅读(58)  评论(0)    收藏  举报