taiga代码研究--base--api--自定义Mixin

 

 

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 {}

 

posted @ 2017-03-27 20:59  八月的男人  阅读(201)  评论(0编辑  收藏  举报