1

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?

3 Answers 3

1

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")
       -------
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.