[Django_Rest_Framework] Error issues collection

Rex Chiang
Oct 5, 2021

--

AssertionError: base_name argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute.

  • According to the official document like below. If you set the get_queryset instead of queryset, then you should set the basename in your viewset url.
basename - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the queryset attribute of the viewset, if it has one. Note that if the viewset does not include a querysetattribute then you must set basename when registering the viewset.
  • views.py
class UserView(viewsets.ModelViewSet):
serializer_class = UserSerializer

#queryset = User.objects.all()

def get_queryset(self):
queryset = User.objects.all()
return queryset
  • urls.py
router = routers.DefaultRouter()
router.register(r'users', views.UserView, basename='UserModel')

--

--

No responses yet