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

Python 3.xDjango Rest-FrameworkDjango Views

Python 3.x Problem Overview


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?

Python 3.x Solutions


Solution 1 - Python 3.x

In the latest DRF, you need to explicitly set base_name in your viewset url if you don't have queryset defined.

So, something like this should do good:

router.register(r'my-model/', MyModelView, basename='MyModel')

See this: docs Hope it helps.

Solution 2 - Python 3.x

You must add an argument called basename for the register method in the url.py file, Like the following code in url.py :

"In url.py"
    
    
...
    
from rest_framework import routers
       
router = routers.DefaultRouter()
router.register(r'my-model/' , MyModelView , basename='MyModel')  
urlpattern=[...]     

Solution 3 - Python 3.x

You need to set basename attribute in your url conf. Docs here

Solution 4 - Python 3.x

in my case i'm using rest framework Default router and changing view name to exact match with model name solved the problem.

View:

class DailyQuote(ModelViewSet):
   queryset = DailyQuote.objects.all()
   serializer_class = DailyQuoteSerializer

Model:

class DailyQuote(models.Model):
   quote = models.TextField()
   text = models.TextField()

so just change MyModelView to Model.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwoe-dev.View Question on Stackoverflow
Solution 1 - Python 3.xJahongir RahmonovView Answer on Stackoverflow
Solution 2 - Python 3.xOmid OstovariView Answer on Stackoverflow
Solution 3 - Python 3.xAK47View Answer on Stackoverflow
Solution 4 - Python 3.xMahyar SalamatView Answer on Stackoverflow