class CreateModelMixin: """ Create a model instance. """ def create(self, request, *args, **kwargs): validator = self.get_validator(data=request.DATA, files=request.FILES) if validator.is_valid(): self.check_permissions(request, 'create', validator.object) self.pre_save(validator.object) self.pre_conditions_on_save(validator.object) self.object = validator.save(force_insert=True) self.post_save(self.object, created=True) instance = self.get_queryset().get(id=self.object.id) serializer = self.get_serializer(instance) headers = self.get_success_headers(serializer.data) return response.Created(serializer.data, headers=headers) return response.BadRequest(validator.errors) def get_success_headers(self, data): try: return {'Location': data[api_settings.URL_FIELD_NAME]} except (TypeError, KeyError): return {}