Remove AutoRouter. (Adding shortcut to generic views/viewsets means it's unneccessary)

This commit is contained in:
Tom Christie 2013-04-29 12:46:57 +01:00
parent dc7b1d6430
commit d17e2d852f
2 changed files with 0 additions and 42 deletions

View File

@ -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> <tr><td>POST</td><td>@action decorated method</td></tr>
</table> </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 # 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. 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.

View File

@ -17,11 +17,9 @@ from __future__ import unicode_literals
from collections import namedtuple from collections import namedtuple
from django.conf.urls import url, patterns from django.conf.urls import url, patterns
from django.db import models
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.reverse import reverse from rest_framework.reverse import reverse
from rest_framework.viewsets import ModelViewSet
from rest_framework.urlpatterns import format_suffix_patterns from rest_framework.urlpatterns import format_suffix_patterns
@ -218,27 +216,3 @@ class DefaultRouter(SimpleRouter):
urls = format_suffix_patterns(urls) urls = format_suffix_patterns(urls)
return 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)