1

I'm building a Django application that exposes a REST API by which users can query my application's models. I'm following the instructions here.

My Model looks like this:

class MyObject(models.Model):
    name = models.TextField()

My Route looks like this in myApp's url.py:

from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'myObjects/(?P<id>\d+)/?$', views.MyObjectsViewSet)
url(r'^api/', include(router.urls)),

My Serializer looks like this:

class MyObjectSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = MyObject
    fields = ('id', 'name',)

My Viewset looks like this:

class MyObjectsViewSet(viewsets.ViewSet):

    def retrieve(self,request,pk=None):
        queryset = MyObjects.objects.get(pk=pk).customMyObjectList()

        if not queryset:
            return Response(status=status.HTTP_400_BAD_REQUEST)
        else:
            serializer = MyObjectSerializer(queryset)
            return Response(serializer.data,status=status.HTTP_200_OK)

When I hit /api/myObjects/60/ I get the following error:

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

This says that I need a base_name parameter on my route. But from the docs, it is unclear to me what that value of that base_name parameter should be. Can someone please tell me what the route should look like with the base_name?

1

1 Answer 1

0

Your viewset is missing a .queryset or .model property. DefaultRouter() introspects one of these properties to determine the url path.

You can optionally define base_name when creating DefaultRouter():

router = routers.DefaultRouter()
router.register('myObjects', views.MyObjectsViewSet, 'base-name-here')
Sign up to request clarification or add additional context in comments.

4 Comments

Can I put anyhting in the base_name? what should its value be? Can I put a random string? The whole point of my question is what is the point of this base_name and what does it do? How does it work?
I'm not able to explain it any better than the documentation you've linked to.
So in this case. what is the correct value of base_name?
Why not just define a model on your viewset and then you won't have to worry about base_name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.