[Answered ]-Error with serialization in django rest framework

1πŸ‘

βœ…

I’v solved my problem. There is code.

urls.py

from django.conf.urls import patterns, include, url
from views import BookList, BookDetail
from rest_framework.urlpatterns import format_suffix_patterns

from django.contrib import admin
admin.autodiscover()


urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^book/$',  BookList.as_view(), name='book-list'),
    url(r'^book/(?P<pk>\d+)/$', BookDetail.as_view(), name='book-detail'),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)

urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'api'])

views.py

class BookList(generics.ListCreateAPIView):
    serializer_class = BookSerializer
    def get_queryset(self):
        queryset = Book.objects.filter(user=self.request.user)
        return queryset.order_by('-id')

class BookDetail(generics.RetrieveUpdateDestroyAPIView):
    model = Book
    serializer_class = BookSerializer

1πŸ‘

πŸ‘€alko

Leave a comment