I have a tour and a group model. Whenever a new group gets created I would like to adjust the tour size fields.
If I understood correctly, the dispatch() method is the right place for this.
However I receive following error:
AttributeError at /booking/newgroup/
'WSGIRequest' object has no attribute 'data'
def reduceTourSize(id, pers):
t = Tour.objects.get(id=id)
t.available_size = t.available_size - int(pers)
t.current_size = t.current_size + int(pers)
t.save()
return t.current_size
class NewGroup(generics.CreateAPIView):
serializer_class = GroupListSerializer
queryset = Group.objects.all()
def dispatch(self, request, *args, **kwargs):
myresult = reduceTourSize(request.data['tour'], request.data['persons'])
return Response(data={"current_size": myresult})
So how can I access the parameters passed in via POST?
Shouldn't you be accessing POST data inside the post method?
class NewGroup(generics.CreateAPIView):
serializer_class = GroupListSerializer
queryset = Group.objects.all()
def post(self, request, *args, **kwargs):
name = request.data.get("name")
-------
You don't need to use dispatch here, check behavior, that CreateModelMixin provide you. It call save() method in serializer that call create() method in serializer, so you need to override this method. In create method you can get data from validated_data parameter.
The solution from #SDRJ works fine:
class NewGroup(generics.CreateAPIView):
serializer_class = GroupListSerializer
queryset = Group.objects.all()
def post(self, request, *args, **kwargs):
myresult = reduceTourSize(request.data['tour'], request.data['persons'])
return self.create(request, *args, **kwargs)
I am attempting to override the update method for a put request in django rest framework. Instead of returning just the updated object. I want it to return the entire queryset including the updated object. For the use case I am working on, its just easier.
Starting from the top.
I am using Django Rest Frameworks Generics.
class SearchCityDetail(RetrieveUpdateDestroyAPIView):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
I override the classes PUT method and inherent from my custom mixin.
class SearchCityDetail(RetrieveUpdateDestroyAPIView, UpdateReturnAll):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
def put(self, request, *args, **kwargs):
return self.updatereturnall(self,request, *args, **kwargs)
the custom mixin looks like this (my custom added code which differs from the normal update code had the commment #Custom Code above it:
from rest_framework import status
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.mixins import UpdateModelMixin
"""
Update a model instance and return all.
"""
#Custom Code
class UpdateReturnAll(UpdateModelMixin):
#custom name normally just update
def updatereturnall(self, request, model, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
# If 'prefetch_related' has been applied to a queryset, we need to
# forcibly invalidate the prefetch cache on the instance.
instance._prefetched_objects_cache = {}
#all objects and all serializer is custom code
allobjects = self.get_queryset(self)
allserializer = self.get_serializer(allobjects, many=True)
return Response(allserializer.data)
def perform_update(self, serializer):
serializer.save()
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
The self.get_queryset and the self.get_serializer are functions defined in the GenericAPIView which RetrieveUpdateDestroyAPIView inherents from. And since I am inheriting the UpdateReturnAll into the SearchCityDetail class the two methods should be available to the UpdateReturnALL
that is my understanding.
I am currently getting an error and status code 500
the error being: AttributeError: 'SearchCityDetail' object has no attribute 'data'
what am I doing wrong?
It should be:
class SearchCityDetail(RetrieveUpdateDestroyAPIView, UpdateReturnAll):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
def put(self, request, *args, **kwargs):
return self.updatereturnall(request, *args, **kwargs)
Instead of return self.updatereturnall(self,request, *args, **kwargs).
Don't need to pass self argument explicit when call self.updatereturnall method, Python do it for you.
In DRF, I have a simple ViewSet like this one:
class MyViewSet(viewsets.ViewSet):
def update(self, request):
# do things...
return Response(status=status.HTTP_200_OK)
When I try a PUT request, I get an error like method PUT not allowed. If I use def put(self, request): all things work fine. Accordingly to the docs I should use def update(): not def put():, why does it happen?
PUT needs id in URL by default
Sometimes there is the difference between POST and PUT, because PUT needs id in URL
That's why you get the error: "PUT is not Allowed".
Example:
POST: /api/users/
PUT: /api/users/1/
Hope it'll save a lot of time for somebody
Had a similar "Method PUT not allowed" issue with this code, because 'id' was missing in the request:
class ProfileStep2Serializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('middle_initial', 'mobile_phone', 'address', 'apt_unit_num', 'city', 'state', 'zip')
class Step2ViewSet(viewsets.ModelViewSet):
serializer_class = ProfileStep2Serializer
def get_queryset(self):
return Profile.objects.filter(pk=self.request.user.profile.id)
Turned out that i have missed 'id' in the serializer fields, so PUT request was NOT able to provide an id for the record. The fixed version of the serializer is below:
class ProfileStep2Serializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('id', 'middle_initial', 'mobile_phone', 'address', 'apt_unit_num', 'city', 'state', 'zip')
This answer is right, Django REST framework: method PUT not allowed in ViewSet with def update(), PUT is not allowed, because DRF expects the instance id to be in the URL. That being said, using this mixin in your ViewSet is probably the best way to fix it (from https://gist.github.com/tomchristie/a2ace4577eff2c603b1b copy pasted below)
class AllowPUTAsCreateMixin(object):
"""
The following mixin class may be used in order to support PUT-as-create
behavior for incoming requests.
"""
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object_or_none()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
if instance is None:
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
lookup_value = self.kwargs[lookup_url_kwarg]
extra_kwargs = {self.lookup_field: lookup_value}
serializer.save(**extra_kwargs)
return Response(serializer.data, status=status.HTTP_201_CREATED)
serializer.save()
return Response(serializer.data)
def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)
def get_object_or_none(self):
try:
return self.get_object()
except Http404:
if self.request.method == 'PUT':
# For PUT-as-create operation, we need to ensure that we have
# relevant permissions, as if this was a POST request. This
# will either raise a PermissionDenied exception, or simply
# return None.
self.check_permissions(clone_request(self.request, 'POST'))
else:
# PATCH requests where the object does not exist should still
# return a 404 response.
raise
This is because the APIView has no handler defined for .put() method so the incoming request could not be mapped to a handler method on the view, thereby raising an exception.
(Note: viewsets.ViewSet inherit from ViewSetMixin and APIView)
The dispatch() method in the APIView checks if a method handler is defined for the request method.If the dispatch() method finds a handler for the request method, it returns the appropriate response. Otherwise, it raises an exception MethodNotAllowed.
As per the source code of dispatch() method in the APIView class:
def dispatch(self, request, *args, **kwargs):
...
...
try:
self.initial(request, *args, **kwargs)
# Get the appropriate handler method
if request.method.lower() in self.http_method_names:
# here handler is fetched for the request method
# `http_method_not_allowed` handler is assigned if no handler was found
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
response = handler(request, *args, **kwargs) # handler is called here
except Exception as exc:
response = self.handle_exception(exc)
self.response = self.finalize_response(request, response, *args, **kwargs)
return self.response
Since .put() method handler is not defined in your view, DRF calls the fallback handler .http_method_not_allowed. This raises an MethodNotAllowed exception.
The source code for .http_method_not_allowed() is:
def http_method_not_allowed(self, request, *args, **kwargs):
"""
If `request.method` does not correspond to a handler method,
determine what kind of exception to raise.
"""
raise exceptions.MethodNotAllowed(request.method) # raise an exception
Why it worked when you defined .put() in your view?
When you defined def put(self, request): in your view, DRF could map the incoming request method to a handler method on the view. This led to appropriate response being returned without an exception being raised.
def update(self, request, pk=None):
data_in = request.data
print(data_in)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=False)
serializer.is_valid(raise_exception=True)
if instance is None:
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
lookup_value = self.kwargs[lookup_url_kwarg]
extra_kwargs = {self.lookup_field: lookup_value}
serializer.save(**extra_kwargs)
return Response(serializer.data, status=status.HTTP_201_CREATED)
serializer.save()
data_out = serializer.data
return Response(serializer.data)
Using Django viewsets you can easily map the method in the url e.g.
path('createtoken/', CreateTokenView.as_view({'post': 'create', 'put':'update'}))
Then in your class override the methods as you please:
class CreateTokenView(viewsets.ModelViewSet):
queryset = yourSet.objects.all()
serializer_class = yourSerializer
def create(self, request, *args, **kwargs):
#any method you want here
return Response("response")
def update(self, request, *args, **kwargs):
# any method you want here
return Response("response")
I have multiple objects that are working with ModelViewSet and all have different (unique) fields for lookup.
So I came up with another solution for this question by defining put on a parent class and a new field lookup_body_field that can be used to associate payload with existing object:
class CustomViewSet(viewsets.ModelViewSet):
lookup_body_field = 'id'
def put(self, pk=None):
lookup_value = self.request.data.get(self.lookup_body_field)
if not lookup_value:
raise ValidationError({self.lookup_body_field: "This field is mandatory"})
obj = self.get_queryset().filter(**{self.lookup_body_field: lookup_value}).last()
if not obj:
return self.create(request=self.request)
else:
self.kwargs['pk'] = obj.pk
return self.update(request=self.request)
class MyViewSetA(CustomViewSet)
model = ModelA
lookup_body_field = 'field_a' # Unique field on ModelA
class MyViewSetB(CustomViewSet)
model = ModelB
lookup_body_field = 'field_b' # Unique field on ModelA
Suppose
you have registered a route like this (in urls.py)
router = DefaultRouter()
router.register(r'users', UserViewSet, basename='user-viewset')
urlpatterns += router.urls
and your restapi routs starts with /api/.
ViewSets generates follow routs.
create a user (send user object in body)
POST
/api/users/
get list of users
GET
/api/users/
get a user
GET
/api/users/{id}/
update a user (full object update)
PUT
/api/users/{id}/
partial update for a user
PATCH
/api/users/{id}/
delete a user
DELETE
/api/users/{id}/
I am facing an issue today, here is my Django class based view and decorator.
def my_decorator(view_func):
def _decorator(request, *args, **kwargs):
print "I am here"
# maybe do something before the view_func call
response = view_func(request, *args, **kwargs)
# maybe do something after the view_func call
return response
return wraps(view_func)(_decorator)
class ConfigletList(APIView):
"""
List all Configlets, or create a new Configlet.
"""
#my_decorator
def get(self, request, format=None):
configlets = Configlet.objects.filter(status = True).order_by('name')
serializer = ConfigletSerializer(configlets, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = ConfigletSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
From get request I am calling a decorator to access request object in _decorator function. But it is not getting called. Please do let me know what is the issue in this code. I never faced any such issue in decorator before.
Is there a established way that i validate an object in the dispatch without making an extra database call when self.get_object() is called later in get/post?
Here is what i have so far (slightly altered for this question):
class CourseUpdateView(UpdateView):
def dispatch(self, request, *args, **kwargs):
self.request = request
self.kwargs = kwargs
self.object = self.get_object()
if self.object.is_online:
messages.warning(request, "Sorry this one can't be updated")
return redirect("course:detail", pk=self.kwargs['pk'])
# this is going to call self.get_object again isn't it?
return UpdateView.dispatch(self, request, *args, **kwargs)
You can cache the result of get_object().
Here's a trivial example:
class CourseUpdateView(UpdateView):
# [...] your dispatch method
def get_object(self):
# it doesn't matter how many times get_object is called per request
# it should not do more than one request
if not hasattr(self, '_object'):
self._object = super(CourseUpdateView, self).get_object()
return self._object