我试图重构我的django项目。 因此,我想重构:
@api_view([GET, POST])
@permission_classes((IsAuthenticated, VehiclePermissions, ))
def inactive_vehicle_view(request):
if request.method == "GET":
con = CBaseUtil.get_prod_instance()
vehicle_bo = VehicleBO()
dongle_dao = OBDDongleDAO(con)
since_days = int(request.GET.get("since", 28))
vehicles = vehicle_bo.find_vehicles_by_user_context(request.user.details)
return Response(vehicles, status=status_code, headers=get_headers(request))
基于类的视图:
class InactiveVehicleView(View):
@authentication_classes((BasicAuthentication, WebsiteAuthentication))
@permission_classes((IsAuthenticated, VehiclePermissions, ))
def dispatch(self, *args, **kwargs):
return super(InactiveVehicleView, self).dispatch(*args, **kwargs)
def get(self, request):
con = CBaseUtil.get_prod_instance()
vehicle_bo = VehicleBO()
dongle_dao = OBDDongleDAO(con)
since_days = int(request.GET.get("since", 28))
vehicles = vehicle_bo.find_vehicles_by_user_context(request.user.details)
return Response(vehicles, status=status_code, headers=get_headers(request))
我面临的问题是,我无法通过request.user.details获取旧版本中的用户详细信息,因为WSGI-Request不包含属性用户。我想我的装饰师做错了,但我无法弄清楚。
FYI视图映射在这样的网址中:
url(r'^vehicles/inactive/?$', InactiveVehicleView.as_view())
有没有人知道我对身份验证和/或装饰器做错了什么?
答案 0 :(得分:3)
根据Django Rest Framework Authentication Docs,您的视图应该是APIView
的子类,并将authentication_classes
和permission_classes
设置为属性,而不是使用装饰器。
from rest_framework.views import APIView
class InactiveVehicleView(APIView):
authentication_classes = (BasicAuthentication, WebsiteAuthentication)
permission_classes = (IsAuthenticated, VehiclePermissions)
def get(self, request):
...