2016-07-08 17 views
1

私はDjangoRestMultipleModelsを使用して、私のMultipleModelAPIViewこの方法を登録しよう:MultipleModelAPIViewの登録方法は?

router = DefaultRouter() 
router.register(r'category-filter', rest.CategoryFilterViewSet, base_name='category-filter') 
urlpatterns = router.urls 

そして、ここでは私のビューセットです:

@permission_classes((permissions.AllowAny,)) 
class CategoryFilterViewSet(MultipleModelAPIView): 
    def get_queryset(self): 
     category = self.request.query_params['category'] 
     queryList = [ 
      (models.Region.objects.filter(city__category__isnull=False), RegionsSerializer, 'regions'), 
      (models.Category.objects.filter(level=1, parent__alias=category), CategoriesSerializer, 'categories'), 
     ] 
     return queryList 

私のサーバーがエラーで始まっ:

TypeError: as_view() takes exactly 1 argument (3 given) 

そして、クラッシュします。ここに完全なトレースバックがあります:

Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace instance at 0x7f14980e1d40> 
Traceback (most recent call last): 
    File "/opt/pycharm-2016.1.4/helpers/pydev/_pydev_bundle/pydev_monkey.py", line 553, in __call__ 
    return self.original_func(*self.args, **self.kwargs) 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper 
    fn(*args, **kwargs) 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run 
    self.check(display_num_errors=True) 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check 
    include_deployment_checks=include_deployment_checks, 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks 
    new_errors = check(app_configs=app_configs) 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config 
    return check_resolver(resolver) 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver 
    for pattern in resolver.url_patterns: 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns 
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module 
    return import_module(self.urlconf_name) 
    File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/home/dukeimg/PycharmProjects/dorogi/dorogi/urls.py", line 24, in <module> 
    url(r'^api/', include('activities.api')), 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include 
    urlconf_module = import_module(urlconf_module) 
    File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/home/dukeimg/PycharmProjects/dorogi/activities/api.py", line 9, in <module> 
    urlpatterns = router.urls 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/rest_framework/routers.py", line 79, in urls 
    self._urls = self.get_urls() 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/rest_framework/routers.py", line 317, in get_urls 
    default_urls = super(DefaultRouter, self).get_urls() 
    File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/rest_framework/routers.py", line 256, in get_urls 
    view = viewset.as_view(mapping, **route.initkwargs) 
TypeError: as_view() takes exactly 1 argument (3 given) 

私は間違っていますか?

答えて

1

router.register(r'category-filter', rest.CategoryFilterViewSet, base_name='category-filter') 

を取り除くと、デフォルトのルータは、通常のビューセットとしてこれを治療するための方法を知らないよう

urlpatterns = [ 
     ... # your other urls 
     url(r'^category-filter/', rest.CategoryFilterViewSet), 
     ... 
] 

を指定して取得します。

+0

私は本当にそれをしたくありません。 – Viktor

+0

urlタプル内のregexpで引数を適切に指定しても、それはできません。 – dmitryro

+0

https://media.readthedocs.org/pdf/django-rest-multiple-models/latest/django-rest-multiple-models.pdf – dmitryro

関連する問題