【问题标题】:Django rest api problem: should either include a `querylist` attribute, or override the `get_querylist()` methodDjango rest api 问题:应该包含 `querylist` 属性,或者覆盖 `get_querylist()` 方法
【发布时间】:2019-05-30 04:47:59
【问题描述】:

这是我的序列化程序类:

class JlistSerializers(serializers.ModelSerializer):
    class Meta:
        model = Jlist
        fields = ('id', 'name', 'news_channel', 'wiki', 'image', 'total_star', 'total_user')

这是我的看法

class JlistView(ObjectMultipleModelAPIView):
    queryset = Jlist.objects.all()

    def get_queryset(self, *args, **kwargs):
        userId = self.kwargs.get('pk')
        queryset = [
            {'queryset': Jlist.objects.all(),
             'serializer_class': JlistSerializers},
            {'queryset': JStarList.objects.filter(userId=userId),
             'serializer_class': JStarList}
        ]
        return queryset

我收到以下错误

AssertionError at /api/jlist
JlistView should either include a `querylist` attribute, or override the `get_querylist()` method.

我使用相同的代码为其他序列化程序类创建 api,但在创建此 api 时出错。请帮我弄清楚这里有什么问题?

【问题讨论】:

    标签: django python-3.x django-rest-framework django-views


    【解决方案1】:

    您定义了 get_queryset,但它应该是 get_querylist。并删除查询集成员。

    class JlistView(ObjectMultipleModelAPIView):
    
        def get_querylist(self, *args, **kwargs):
            userId = self.kwargs.get('pk')
            queryset = [
                {'queryset': Jlist.objects.all(),
                 'serializer_class': JlistSerializers},
                {'queryset': JStarList.objects.filter(userId=userId),
                 'serializer_class': JStarList}
            ]
            return queryset
    

    【讨论】:

    猜你喜欢
    • 2019-10-29
    • 1970-01-01
    • 2017-12-22
    • 2021-03-24
    • 2021-08-07
    • 2017-04-04
    • 1970-01-01
    • 2021-03-27
    • 2016-12-26
    相关资源
    最近更新 更多