Django rest framework - cannot get rights based on class

Here are my codes:

serializers.py

class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ( 'username', 'email') class AllListingSerializer(serializers.ModelSerializer): class Meta: model = Listing fields = ('name', 'desc', 'thumbnail', 'office_no', 'mobile_no', 'email', 'web ') 

views.py

 class UserViewSet(generics.ListCreateAPIView): queryset = User.objects.all() serializer_class = UserSerializer class AllListing(generics.ListCreateAPIView): queryset = Listing.objects.all() serializer_class = AllListingSerializer 

urls.py

 urlpatterns = patterns('', url(r'^$', apiview.UserViewSet), url(r'^listings/$', apiview.AllListing), ) 

But when I get the base url, it shows

init () takes 1 positional argument, but 2 are given

and when I go to '/ listings /' url, I get 404 Page not found, but I have several entries in db.

I am new to django. I can’t understand what is wrong with them. I am using Django 1.7.1 in virtualwrappr, python 3.4.

+6
source share
2 answers

You must call .as_view() for each API view:

 urlpatterns = patterns('', url(r'^$', apiview.UserViewSet.as_view()), url(r'^listings/$', apiview.AllListing.as_view()), ) 

Also consider using the REST Routers framework, which will provide you with a simple, fast, and consistent way to connect your presentation logic to a set of URLs.

+7
source

It happened to me when I expanded generics.GenericAPIView instead viewsets.GenericViewSet in my custom class ViewSet .

Pretty obvious, but easy to miss.

+4
source

Source: https://habr.com/ru/post/978173/


All Articles