The problem is this:
I'm trying to replace the standard queryset:
queryset: MyModel.objects.all()
on my:
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
when I remove the "queryset", and leave only "get_queryset", an error appears:
AssertionError: base_name
argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset
attribute.
All together looks so:
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$&(._)-]+'
so How to override method correctly?
question from:
https://stackoverflow.com/questions/48548622/base-name-argument-not-specified-and-could-not-automatically-determine-the-name 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…