AssertionError:未指定`base_name`参数

时间:2018-03-07 16:45:14

标签: python django django-rest-framework django-views

我正在尝试将模型的参数添加到URL模式中,如下所示:

http://111.111.11.111:8080/resultados/image.jpg

其中 nome_ficheiro = image.jpg (nome_ficheiro是模型参数,见下文)

但是我收到以下错误:

 File "/usr/local/lib/python2.7/dist-packages/rest_framework/routers.py", line 139, in get_default_base_name
    assert queryset is not None, '`base_name` argument not specified, and could ' \
AssertionError: `base_name` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.

网址格式:

    router = routers.DefaultRouter()
    urlpatterns = [url(r'^', include(router.urls))]
    router.register(r'resultados/(?P<nome_ficheiro>.+)/$',resultUploadView.as_view({'get': 'get_queryset'}))

观点:

class resultUploadView(generics.ListAPIView):
    serializer_class = resultSerializer
    def get_queryset(self):
        nome = self.kwargs['nome_ficheiro']
        return labelResult.objects.filter(nome_ficheiro=nome)

模特:

 class labelResult(models.Model):
    nome_ficheiro   = models.CharField(max_length=120)
    especie         = models.CharField(max_length=120)
    zona            = models.CharField(max_length=120)
    data            = models.CharField(max_length=120)

使用:Python 2.7.12和DRF 3.6.3

编辑:

urls.py:

router.register(r'results/(?P<nome_ficheiro>.+)/$', resultUploadView.as_view({'get': 'get_queryset'}), base_name='img_name')

仍然无效,收到同样的错误

2 个答案:

答案 0 :(得分:1)

您可以在添加路线时命名路线:

router.register(
    r'the-url_pattern$',
    TheViewSet,
    base_name='put-something-here',
)

顺便说一句,指定queryset类属性和get_queryset方法都很奇怪,这可能会让DRF感到困惑。选择一种方式或另一种方式。

答案 1 :(得分:1)

将“ base_name”替换为“ basename”