我正在尝试替换 标准查询集:
queryset: MyModel.objects.all()
在我的:
def get_queryset(self, username=None):
if username is not None:
user = UserModel.objects.get(username=username)
queryset = MyModel.filter(author=user)
return queryset
else:
queryset = MyModel.objects.all()
return queryset
当我移除"queryset",只留下"get_queryset"时会出现错误:
断言错误:
base_name
参数未指定,也无法从视图集自动确定名称,因为它没有.queryset
属性。
全部内容如下:
class MyModelView(viewsets.ModelViewSet):
permissions_classes = (permissions.IsAuthenticated,)
serializer_class = MyModelleSerializer
def get_queryset(self, username=None):
if username is not None:
user = UserModel.objects.get(username=username)
queryset = MyModel.filter(author=user)
return queryset
else:
queryset = MyModel.objects.all()
return queryset
lookup_field = 'username'
lookup_value_regex = '[a-zA-Z0-9$&(._)\-]+'
如何正确地覆盖方法?