I'm learning DRF and feel confused a little now.
I set up QuestionView
and QuestionSerializer
like this.
views.py
class QuestionView(viewsets.ModelViewSet) :
queryset = models.Question.objects.all()
serializer_class = QuestionSerializer
def list(self, request, *args, **kwargs):
serializer = QuestionSerializer(models.Question.objects.all())
return Response(serializer.data)
serializer.py
class QuestionSerializer(serializers.ModelSerializer):
class Meta:
model= Question
fields= ("question_text", "owner", "pub_date")
urls.py
router = routers.DefaultRouter()
router.register('profile', cebula_views.SettingView)
router.register('question', cebula_views.QuestionView.as_view({
'get':'list',
}), 'userpage-question')
urlpatterns = [
...
url(r'^', include(router.urls)),
...
]
File "C:\Users\1Sun\Cebula3\businessproject\urls.py", line 34, in url(r'^', include(router.urls)), File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\routers.py", line 101, in urls self._urls = self.get_urls() File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\routers.py", line 363, in get_urls urls = super(DefaultRouter, self).get_urls() File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\routers.py", line 261, in get_urls routes = self.get_routes(viewset) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\routers.py", line 176, in get_routes extra_actions = viewset.get_extra_actions() AttributeError: 'function' object has no attribute 'get_extra_actions'
In my opinion, It is that I should write get_extra_actions
method in QuestionView
class, If so, how I write that? like this?
def get_extra_actions() :
return ???
If not, what is the problem?
[Edit]
when I erase .as_view() at the url part and write like router.register('question', cebula_views.QuestionView, 'userpage-question')
the error is
Unhandled exception in thread started by .wrapper at 0x00000283172D3D08> Traceback (most recent call last): File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run self.check(display_num_errors=True) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 364, in check include_deployment_checks=include_deployment_checks, File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 351, in _run_checks return checks.run_checks(**kwargs) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks new_errors = check(app_configs=app_configs) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config return check_resolver(resolver) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver return check_method() File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 397, in check for pattern in self.url_patterns: File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 36, in get res = instance.dict[self.name] = self.func(instance) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 536, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 36, in get res = instance.dict[self.name] = self.func(instance) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 529, in urlconf_module return import_module(self.urlconf_name) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 665, in _load_unlocked
File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed File "C:\Users\1Sun\Cebula3\businessproject\urls.py", line 31, in url(r'^cebula/',include('cebula.urls',namespace='cebula')), File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\conf.py", line 34, in include urlconf_module = import_module(urlconf_module) File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 665, in _load_unlocked
File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed File "C:\Users\1Sun\Cebula3\cebula\urls.py", line 15, in url(r'^question/$',views.QuestionView.as_view(),name='question'), File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\viewsets.py", line 68, in as_view raise TypeError("Theactions
argument must be provided when " TypeError: Theactions
argument must be provided when calling.as_view()
on a ViewSet. For example.as_view({'get': 'list'})
router.register('question', cebula_views.QuestionView, 'userpage-question')
the error is > url(r'^question/$',views.QuestionView.as_view(),name='question'), File "C:\Users\1Sun\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\viewsets.py", line 68, in as_view raise TypeError("Theactions
argument must be provided when " TypeError: Theactions
argument must be provided when calling.as_view()
on a ViewSet. For example.as_view({'get': 'list'})
– Villainous