Django rest框架-调用另一个基于类的视图 [英] Django rest framework- calling another class-based view

查看:249
本文介绍了Django rest框架-调用另一个基于类的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仔细研究了几篇类似的文章(以及从同一项目中的另一个应用程序中调用一个应用程序的基于类的视图似乎很有希望,但不起作用),但有些功能较老,对我来说却没有用。这是我的设置(使用Django == 2.0.6,djangorestframework == 3.8.2)

I have pored over several similar posts (and Calling a class-based view of an app from another app in same project seemed promising, but does not work), but some are older and none quite work for me. Here's my setup (using Django==2.0.6, djangorestframework==3.8.2)

我有一个基本模型(在这里简化):

I have a basic model (simplified here):

from django.db import models
class Resource(models.Model):
    name = models.CharField(max_length=100, null=False)

我有一个基本端点,可以在其中列出和创建资源实例:

I have a basic endpoint where I can list and create Resource instances:

from rest_framework import generics, permissions
from myapp.models import Resource
from myapp.serializers import ResourceSerializer

class ListAndCreateResource(generics.ListCreateAPIView):
    queryset = Resource.objects.all()
    serializer_class = ResourceSerializer
    permission_classes = (permissions.IsAuthenticated,)

(afaik,序列化程序的详细信息无关,所以

(afaik, the details of the serializer are not relevant, so that is left out).

无论如何,除了该基本终结点,我还有另一个API终结点一些操作,但还会在此过程中创建一些资源对象。当然,我想利用 ListAndCreateResource 类中封装的功能,因此我只需要维护一个地方 Resource s已创建。

Anyway, in addition to that basic endpoint, I have another API endpoint which performs some actions, but also creates some Resource objects in the process. Of course, I would like to make use of the functionality encapsulated in the ListAndCreateResource class so I only have to maintain one place where Resources are created.

我尝试过:

尝试1:

class SomeOtherView(generics.CreateAPIView):
    def post(self, request, *args, **kwargs):
        # ... some other functionality...
        # ...
        response = ListAndCreateResource().post(request, *args, **kwargs)
        # ... more functionality...
        return Response({'message': 'ok'})

不幸的是,对我不起作用。在我的跟踪中,我得到:

Unfortunately, that does not work for me. In my trace, I get:

  File "/home/projects/venv/lib/python3.5/site-packages/rest_framework/generics.py", line 111, in get_serializer
    kwargs['context'] = self.get_serializer_context()
  File "/home/projects/venv/lib/python3.5/site-packages/rest_framework/generics.py", line 137, in get_serializer_context
    'request': self.request,
AttributeError: 'ListAndCreateResource' object has no attribute 'request'

尝试2:
此尝试尝试使用 as_view 方法,即所有基于Django类的视图的一部分:

Attempt 2: This attempt tries to use the as_view method which is part of all Django class-based views:

class SomeOtherView(generics.CreateAPIView):
    def post(self, request, *args, **kwargs):
        # ... some other functionality...
        # ...
        response = ListAndCreateResource.as_view()(request, *args, **kwargs)
        # ... more functionality...
        return Response({'message': 'ok'})

但是放弃了:

AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`

所以我的问题是 ...有直接的方法吗?我可以访问 rest_framework.request.Request 对象(类型为 _request 属性) > django.http.HttpRequest ,但随后我没有DRF Request 对象中包含的任何身份验证详细信息(实际上,我的如果我使用 response = ListAndCreateResource()。as_view()(request._request,* args,** kwargs) ListAndCreateResource 返回403 c $ c>在上面的尝试#2中)。

So my question is...is there a straightforward way to do this? I can access the _request attribute of the rest_framework.request.Request object (which is of type django.http.HttpRequest, but then I do not have any of the authentication details that are contained in the DRF Request object (indeed, my ListAndCreateResource returns a 403 if I use response = ListAndCreateResource().as_view()(request._request, *args, **kwargs) in attempt #2 above).

预先感谢!

推荐答案

这似乎有点晚了,但万一有人想知道。

This seems a bit late, but in case anyone is wondering.

class SomeOtherView(generics.CreateAPIView):
    def post(self, request, *args, **kwargs):
        # ... some other functionality...
        # ...
        response = ListAndCreateResource.as_view()(request, *args, **kwargs)
        # ... more functionality...
        return Response({'message': 'ok'})

as_view( )是一个函数,该函数在调用时会返回接受请求的函数* args,** kwargs。因此,基本上,类视图是封装的函数视图。

The as_view() is a function that when called, returns a function that takes a request, *args, **kwargs. So basically, a class view is an encapsulated function view.

这篇关于Django rest框架-调用另一个基于类的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆