[Django]-What base_name parameter do I need in my route to make this Django API work?

46👍

Try doing this in your urls.py. The third parameter ‘Person’ can be anything you want.

router.register(r'person/food', views.PersonViewSet, 'Person')

38👍

Let me explain, why we need an base_name in the first place and then let’s go into the possible value of base_name.

If you ever used the Django urls without the rest-framework (DRF) before, you would’ve specified it like this:

urlpatterns = [
    url(r'myObjects/(?P<id>\d+)/?$', views.MyObjectsListView.as_view(), name='myobject-list'),
    url(r'myObjects/(?P<id>\d+)/?$', views.MyObjectsDetailView.as_view(), name='myobject-detail'),
]

Here, if you see, there is a name parameter which used to identify the url in the current namespace (which is app).

This is exactly what django-rest-framework trying to do automatically, since the drf knows whether the view is list or detail (because of viewset). it just needs to append some prefix to differentiate the urls. That’s the purpose of base_name (prefix).

In most scenario, you can give the url or resource name as base_name. In your case, base_name=myobject. DRF will generate base_name + view_type as the name parameter like myobject_list & myobject_detail.

Note: Usually, base_name will be automatically obtained from the queryset field (from view), since it’s same for all view types in a viewset. But if you specify, get_queryset method instead of queryset, it possibly means you’ve different queryset for different view types (like list, detail). So, DRF will ask you to specify a common base_name for all the view types for a resource.

13👍

Maybe you just need to set the base_name parameter for your router with the name of the object: MyObject, in your case.

router.register(r'myObjects/(?P<id>\d+)/?$', views.MyObjectsViewSet, base_name="MyObject")

http://www.django-rest-framework.org/api-guide/routers/#Usage

4👍

this is a useful answer, read for the details.

tl;dr

It is used as the base name of the generated URL patterns (e.g., ‘myobject-detail’ or ‘myobject-list’).

2👍

An alternative solution might be to use a ModelViewSet which will derive the basename automatically from the model.

Just make sure and tell it which model to use:

Because ModelViewSet extends GenericAPIView, you’ll normally need to
provide at least the queryset and serializer_class attributes, or the
model attribute shortcut.

2👍

simply mention this,

queryset = MyObjects.objects.all()

like this,

class MyObjectsViewSet(viewsets.ViewSet):
    queryset = MyObjects.objects.all()

in your corresponding Viewset in views.py instead of mentioning under

def retrieve()…

its worked for me 🙂

0👍

Say you have two functions in your views.py to query a bunch of employees.
One to query just the name(employeenameviewset) and other to query addresses also(employeeinfoviewset).
Then in your urls.py, add like so:

router.register(r'employee_names', views.employeenameviewset, basename="employees")
router.register(r'employee_details', views.employeeinfoviewset, basename="employees")

Using the same basename, the rest of the url will be created automatically by Django like it does in case of URL Conf. In fact this is based on URLConf.

Leave a comment