def post(self, request, *args, **kwargs): cake_form = CakeForm(request.DATA) if not cake_form.is_valid(): raise ValidationError(cake_form.errors) cake = cake_form.save() # You can return 'ResourceResponse's if you need to # use a custom 'HttpResponse' class or pass in specific parameters to # the 'HttpResponse' class's constructor. # For example, in this method we want to return an HTTP 201 (CREATED) # response, with the newly created cake's uri in 'Location' header. # To do this we set the 'response_cls' argument to 'http.HttpCreated' # and add a 'location' key to 'response_kwargs' dict. # This is equilivant to returning "cake_form.save(), created" # In this case, the value passed into the location parameter of our # 'HttpCreated' response will be a callable. When invoked it will be # passed one parameter, the entity created from our cake object. # And, just for fun, let's set 'include_entity' to False. # So again, we'll return HTTP 201 (CREATED), with a Location header, # the X-The-Cake-Is-A-Lie header, and no entity body. return ResourceResponse( include_entity=False, response_cls=http.HttpAccepted, response_kwargs={ 'location': lambda entity: entity.get_resource_uri()})
def post(self, request, *args, **kwargs): cake_form = CakeForm(request.DATA) if not cake_form.is_valid(): raise ValidationError(cake_form.errors) cake = cake_form.save() return cake, True
def post(self, request, *args, **kwargs): cake_form = CakeForm(request.DATA) if not cake_form.is_valid(): raise ValidationError(cake_form.errors) # Return the newly created instance and indicate that # HTTP 201 CREATED should be used in the response. # return object, created (boolean) return cake_form.save(), True
def put(self, request, *args, **kwargs): pk = kwargs['pk'] try: created = False instance = Cake.objects.get(pk=pk) except Cake.DoesNotExist: created = True instance = Cake(id=pk) cake_form = CakeForm(request.DATA, instance=instance) if not cake_form.is_valid(): raise ValidationError(cake_form.errors) # Return the newly created instance and indicate that # HTTP 201 CREATED should be used in the response. # OR # Return the updated instance with HTTP 200 OK return cake_form.save(), created