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',)
}
The ModelViewSet class inherits from GenericAPIView and includes implementations for various actions, by mixing in the behavior of the various mixin classes.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With