本文介绍了"CityListViewSet"应包含"serializer_class"属性,或覆盖"get_serializer_class()"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设标题中有错误,为清楚起见,再次在这里

I am assuming by the error in the title, once more here for clarity

'CityListViewSet' should either include a `serializer_class` attribute,
or override the `get_serializer_class()` method.

我的序列化程序未连接到我的视图,在我的代码中应该如此.我不太确定这个错误在哪里.我想知道你们中是否有人看到过类似的东西?

that my serializer isn't connected to my view, which in my code it should be. I'm not really sure where the bug is in this one. I wonder if any of you have seen something similar?

这是代码.

路由器:

router.register(r'city-list', CityListViewSet, base_name='city-list')

视图:

class CityListViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Venue.objects.values('city').distinct()
    serializer = CitySerializer(queryset, many=True)
    ordering_fields = ('city',)
    ordering = ('city',)

序列化器:

class CitySerializer(serializers.ModelSerializer):
    class Meta:
        model = City
        fields =('city',)

是什么原因导致似乎似乎正确连接了代码的断言错误?

what is it that would be causing such an assertion error with the code seemly wired up correctly?

推荐答案

该异常本身说明.您需要一个 serializer_class 属性.您有 serializer .

The exception says it itself. You need a serializer_class attribute. You have serializer.

这篇关于"CityListViewSet"应包含"serializer_class"属性,或覆盖"get_serializer_class()"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 11:56