Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't PUT ModelViewSet Django Rest Framework

I am not sure why I can't do PUT requests to my ModelViewSet like shown in the documentation however PUT does not work. Any ideas? I have included my view and serializer below.

class UserProfileViewSet(viewsets.ModelViewSet):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer
    filter_fields = ('user', 'id', 'account_type')

class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile`

REST_FRAMEWORK = {
    'DEFAULT_MODEL_SERIALIZER_CLASS':
        'rest_framework.serializers.ModelSerializer',
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
    'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',)
}
like image 406
icebox3d Avatar asked Aug 15 '14 16:08

icebox3d


People also ask

What is ModelViewSet in Django?

The ModelViewSet class inherits from GenericAPIView and includes implementations for various actions, by mixing in the behavior of the various mixin classes.

Can I add Django REST Framework to existing project?

Django Rest Framework is a powerful library that sits on top of existing Django projects to add robust web APIs. If you have an existing Django project with only models and a database--no views, urls, or templates required--you can quickly transform it into a RESTful API with a minimal amount of code.


2 Answers

Is it possible that you are not making the request right? You need to specify the object you are trying to update in the url.

For example, the url needs to include the id of object you are trying to update: http://localhost:8000/api/user/16/

You get the "Method \"PUT\" not allowed." when you don't specify the object in the request.

like image 98
GusGus Avatar answered Oct 16 '22 15:10

GusGus


By what you've shown here it should be working. PUT does not work in List view, only in object Detail view. Try creating an object and look into it's detail view and check if you can see a PUT button there

like image 30
timop Avatar answered Oct 16 '22 13:10

timop