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.
- [Django]-Django accessing ManyToMany fields from post_save signal
- [Django]-'WSGIRequest' object has no attribute 'user' Django admin
- [Django]-Django ALLOWED_HOSTS IPs range
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
- [Django]-'function' object has no attribute 'as_view'
- [Django]-Change a field in a Django REST Framework ModelSerializer based on the request type?
- [Django]-Django py.test does not find settings module
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’).
- [Django]-Django: Purpose of django.utils.functional.SimpleLazyObject?
- [Django]-Choose test database?
- [Django]-How can I see the raw SQL queries Django is running?
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.
- [Django]-How to merge consecutive database migrations in django 1.9+?
- [Django]-How to redo a migration on django 1.8 after using –fake
- [Django]-Is it possible to pass query parameters via Django's {% url %} template tag?
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
- [Django]-How to use Django to get the name for the host server?
- [Django]-Adding REST to Django
- [Django]-What does on_delete do on Django models?
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.
- [Django]-Django template includes slow?
- [Django]-How to obtain a QuerySet of all rows, with specific fields for each one of them?
- [Django]-No module named MySQLdb