简体   繁体   English

Django rest 框架,应该能够覆盖 get_queryset 而不是定义 queryset 属性吗?

[英]Django rest framework, should be able to override get_queryset and not define queryset attribute?

I am confused.我很迷惑。 Looking through the ViewSet source code it looks like I should be able to not define a queryset in a viewset and then just override the get queryset function to get whatever queryset I want.查看 ViewSet 源代码,看起来我应该无法在视图集中定义查询集,然后只需覆盖 get queryset 函数即可获取我想要的任何查询集。 But my code fails with this error:但我的代码失败并出现此错误:

AssertionError: `base_name` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.

So even if I override the queryset attribute I still need to set it to some fake attribute in the beginning... this works, but it feels weird to define the queryset and then just override it a second later.因此,即使我覆盖了 queryset 属性,我仍然需要在开始时将其设置为某个假属性……这是可行的,但是定义查询集然后在一秒钟后覆盖它感觉很奇怪。

class StudyQuestion(viewsets.ReadOnlyModelViewSet):
    queryset = Model.objects.all()
    serializer_class = ModelSerializer
    permission_classes = (permissions.IsAuthenticated, )

    def get_queryset(self):
        """"""
        return Model.objects.order_by('-word__frequency')

A DRF ModelViewSet uses the queryset to derive the URL base.一个DRF ModelViewSet使用queryset导出URL基地。 If the queryset property is not set, DRF asks that you use the optional base_name property when registering the router to declare the base.如果未设置queryset属性,DRF 会要求您在注册路由器时使用可选的base_name属性来声明基。

Check out this page in the DRF docs:在 DRF 文档中查看此页面:

http://www.django-rest-framework.org/api-guide/routers/ http://www.django-rest-framework.org/api-guide/routers/

DRF Router is complaining, because it can't automatically generate a basename for the viewset : DRF 路由器正在抱怨,因为它无法自动为视图集生成基本名称

base_name - The base to use for the URL names that are created. base_name - 用于创建的 URL 名称的基础。 If unset the basename will be automatically generated based on the queryset attribute of the viewset, if it has one.如果未设置 basename 将根据视图集的 queryset 属性自动生成,如果它有的话。 Note that if the viewset does not include a queryset attribute then you must set base_name when registering the viewset.请注意,如果视图集不包含查询集属性,那么您必须在注册视图集时设置 base_name。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django REST Framework - 从 get_queryset 返回一个值? - Django REST Framework - return a value from get_queryset? 如何在 Django Rest Framework 中使用 get_queryset 进行过滤? - How to filter using get_queryset in Django Rest Framework? AssertionError at /api/movies/ 'MovieViewSet' 应该包含一个 `queryset` 属性,或者覆盖 `get_queryset()` 方法 - AssertionError at /api/movies/ 'MovieViewSet' should either include a `queryset` attribute, or override the `get_queryset()` method Django rest 壮观 - 自定义 get_queryset() - Django rest spectacular - Customizing get_queryset() Django REST框架:重写get_queryset()有时会返回一个doubled查询集 - Django REST Framework: overriding get_queryset() sometimes returns a doubled queryset Django 其余框架视图集 get_queryset() 不返回正确的查询集 - Django rest framework viewset get_queryset() doesn't return correct queryset Django Rest Framework错误无法在未设置`.queryset`或没有`.get_queryset()`方法的视图上应用DjangoModelPermissions - django rest framework error Cannot apply DjangoModelPermissions on a view that does not set `.queryset` or have a `.get_queryset()` method django 中的 queryset 属性和 get_queryset() 方法的区别? - Difference between queryset attribute and get_queryset() method in django? 根据django-guardian权限覆盖get_queryset - Override get_queryset based on django-guardian permissions 试图覆盖视图中的 get_queryset - Trying to override a get_queryset in a view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM