mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Remove AutoRouter. (Adding shortcut to generic views/viewsets means it's unneccessary)
This commit is contained in:
parent
dc7b1d6430
commit
d17e2d852f
|
@ -80,22 +80,6 @@ This router is similar to `SimpleRouter` as above, but additionally includes a d
|
|||
<tr><td>POST</td><td>@action decorated method</td></tr>
|
||||
</table>
|
||||
|
||||
## AutoRouter
|
||||
|
||||
The AutoRouter class is similar to the `DefaultRouter` class, except that it doesn't require you to register any viewsets, but instead automatically creates routes for all installed models.
|
||||
|
||||
It can be useful for prototyping, although for anything more advanced you'll want to drop down to using one of the other router classes, and registering viewsets explicitly.
|
||||
|
||||
The following code shows how you can automatically include a complete API for your application with just a few lines of code, using the `AutoRouter` class:
|
||||
|
||||
from django.conf.urls import patterns, url, include
|
||||
from rest_framework.routers import AutoRouter
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^api/', include(AutoRouter().urls)),
|
||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||
)
|
||||
|
||||
# Custom Routers
|
||||
|
||||
Implementing a custom router isn't something you'd need to do very often, but it can be useful if you have specfic requirements about how the your URLs for your API are strutured. Doing so allows you to encapsulate the URL structure in a reusable way that ensures you don't have to write your URL patterns explicitly for each new view.
|
||||
|
|
|
@ -17,11 +17,9 @@ from __future__ import unicode_literals
|
|||
|
||||
from collections import namedtuple
|
||||
from django.conf.urls import url, patterns
|
||||
from django.db import models
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.reverse import reverse
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from rest_framework.urlpatterns import format_suffix_patterns
|
||||
|
||||
|
||||
|
@ -218,27 +216,3 @@ class DefaultRouter(SimpleRouter):
|
|||
urls = format_suffix_patterns(urls)
|
||||
|
||||
return urls
|
||||
|
||||
|
||||
class AutoRouter(DefaultRouter):
|
||||
"""
|
||||
A router class that doesn't require you to register any viewsets,
|
||||
but instead automatically creates routes for all installed models.
|
||||
|
||||
Useful for quick and dirty prototyping.
|
||||
"""
|
||||
def __init__(self):
|
||||
super(AutoRouter, self).__init__()
|
||||
for model in models.get_models():
|
||||
prefix = model._meta.verbose_name_plural.replace(' ', '_')
|
||||
basename = model._meta.object_name.lower()
|
||||
classname = model.__name__
|
||||
|
||||
DynamicViewSet = type(
|
||||
classname,
|
||||
(ModelViewSet,),
|
||||
{}
|
||||
)
|
||||
DynamicViewSet.model = model
|
||||
|
||||
self.register(prefix, DynamicViewSet, basename)
|
||||
|
|
Loading…
Reference in New Issue
Block a user