2

I'm trying to build simple API with Django REST Framework running on Python 3.4.3. However, drf router urls turns out to be empty:

Project tree:

.
├── api
│   ├── profile
│   │   ├── serializers.py
│   │   └── views.py
│   └── urls.py
└── config
    ├── settings
    └── urls.py

Response from server: (it doesn't contain link to the viewset!)

GET /api/
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{}

./config/urls.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView

from app_profile.views import ProfileView

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name="index.html")),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api/', include("api.urls", namespace='api')),
]

./api/urls.py:

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from api.profile.views import ProfileViewSet
from rest_framework.routers import DefaultRouter
from django.conf.urls import include, url

router = DefaultRouter()
router.register('users', ProfileViewSet, 'user')
urlpatterns = [
    url(r'', include(router.urls)),
]

./api/profile/views.py:

from django.shortcuts import render
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from app_profile.models import Profile

from .serializers import ProfileSerializer


class ProfileViewSet(ViewSet):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer

./api/profile/serializers.py

from rest_framework.serializers import ModelSerializer
from django.contrib.auth.models import User
from app_profile.models import Profile

class ProfileSerializer(ModelSerializer):
    class Meta:
        model = Profile

I encounter the same problem with different models. I also tried to check what does router variable contain:

>>> print(router.urls)
[<RegexURLPattern api-root ^$>, <RegexURLPattern api-root ^\.(?P<format>[a-z0-9]+)/?$>]
>>> print(router.registry)
[('users', <class 'api.profile.views.ProfileViewSet'>, 'user')]
>>> 

And this is pretty mind-boggling. Could anyone tell me what am I doing wrong?

1 Answer 1

3

if you're inheriting from ViewSet - you need to specifically define the methods in the class

For your question , it would be better to import from ModelViewSet

from rest_framework import viewsets


class ProfileViewSet(viewsets.ModelViewSet):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer

http://www.django-rest-framework.org/api-guide/viewsets/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.