Django Rest-自定义序列化程序/查询-断言queryset不为None

时间:2019-04-18 16:08:08

标签: django django-rest-framework

我试图在视图集查询集中手动创建自定义序列化器和基于字典ive构建的查询,但是却收到错误,这无助于我识别视图或序列化器的哪一部分失败

File "/itapp/itapp/api/urls.py", line 12, in <module>
    router.register(r'ss_monitoring_data', views.SiteSupernetMontioringDataROView)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/routers.py", line 102, in register
    basename = self.get_default_basename(viewset)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/routers.py", line 185, in get_default_basename
    assert queryset is not None, '`basename` argument not specified, and could ' \
AssertionError: `basename` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.

这是我的序列化器:

class SiteSupernetMonitoringDataROSSerializer(serializers.Serializer):
      site = serializers.CharField(max_length=50)
      subnet = serializers.CharField(max_length=50)
      mask = serializers.CharField(max_length=50)
      circuits = serializers.CharField()

这是视图:

class SiteSupernetMontioringDataROView(generics.ListAPIView):
    permission_classes = (IsAdminUser,) 
    serializer_class = SiteSupernetMonitoringDataROSSerializer
    paginate_by = 20

    def get_queryset(self):
        site_supernet_data = SiteSupernet.objects.filter(site__is_live=True, subnet_type__subnet_type__icontains='site aggregate').values()
        dcs_data = DeviceCircuitSubnets.objects.filter(
                                                    device__site__is_live=True, 
                                                    circuit__decommissioned=False
                                                ) \
                                                .select_related(
                                                    'device'
                                                    'device__site',
                                                    'subnet'
                                                    'circuit'
                                                ).values(
                                                    'id',
                                                    'subnet_id',
                                                    'circuit_id',
                                                    'device_id',
                                                    'device__site_id',
                                                    'circuit__name',
                                                    'subnet__subnet',
                                                    'subnet__mask'
                                                )

        for supernet in site_supernet_data:
            if 'circuits' not in supernet.keys():
                supernet['circuits']= []
            for dcs in dcs_data:
                    if dcs['device__site_id'] == supernet['site_id']:
                        supernet['circuits'].append(dcs)

        return site_supernet_data

1 个答案:

答案 0 :(得分:1)

您正在尝试在路由器中注册SiteSupernetMontioringDataROView。这不是正确的方法。 ListAPIView不能与routers一起使用。您必须使用as_view方法将其传递给网址格式。

urlpatterns = [
    ...
    url('<YOUR_URL>', SiteSupernetMontioringDataROView.as_view())
]

详细了解为您的django版本定义urls here