AssertionError: `basename` argument not specified, and could not automatically determine the name fr

报错解决:AssertionError: `basename` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.

因为对应的视图函数没有获取数据库数据,比如这种:

queryset = models.News.objects.filter(deleted=False).order_by('-id')

如果获取了数据库数据,代码就会默认的拿数据对象名字的小写,然后放在url处,源码如下:

     def get_default_basename(self, viewset):
        """
        If `basename` is not specified, attempt to automatically determine
        it from the viewset.
        """
        queryset = getattr(viewset, 'queryset', None)

        assert queryset is not None, '`basename` argument not specified, and could ' \
            'not automatically determine the name from the viewset, as ' \
            'it does not have a `.queryset` attribute.'

        return queryset.model._meta.object_name.lower()

所以解决方法就是我们自己在url中定义一个名字,例如:


原:router.register(r'register', account.RegisterView)

现:router.register(r'register', account.RegisterView, 'register')

再次运行就不报错了。

补充:通过名字反向生成url部分的Basename,就是我们刚定义的url的名字

name='{basename}-list',
name='{basename}-{url_name}',
name='{basename}-detail',

  • 14
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值