当前位置: 首页>>代码示例>>Python>>正文


Python UserSerializer.get_fields方法代码示例

本文整理汇总了Python中serializers.UserSerializer.get_fields方法的典型用法代码示例。如果您正苦于以下问题:Python UserSerializer.get_fields方法的具体用法?Python UserSerializer.get_fields怎么用?Python UserSerializer.get_fields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在serializers.UserSerializer的用法示例。


在下文中一共展示了UserSerializer.get_fields方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: post

# 需要导入模块: from serializers import UserSerializer [as 别名]
# 或者: from serializers.UserSerializer import get_fields [as 别名]
    def post(self, request, format=None, sport=None):
        try:
            email = request.data['email']
            existing_user = User.objects.get(email=email)
            return Response(error_response('User with email %s already exists.'
                                           % email),
                            status=status.HTTP_400_BAD_REQUEST)
        except:
            pass

        sport = int(request.query_params.get('sport', '0'))
        user = UserSerializer()
        user_fields = [key for key in user.get_fields().keys()]
        user_data = { key: request.data[key]
                      for key in user_fields
                      if key in request.data }
        user_data['type_id'] = SPORTS[sport][1] 
        user_serializer = UserSerializer(data=user_data)

        # Create base user
        if user_serializer.is_valid():
            if not 'password' in request.data:
                return Response(error_response('Password not provided.'),
                                status=status.HTTP_400_BAD_REQUEST)
            else:
                user = user_serializer.save()
                user.set_password(request.data['password'])
                user.save()

        # Create specific user (swimmer, runner...)
        if sport == 0:
            swimmer = SwimmerSerializer()
            swimmer_fields = [key for key in SwimmerSerializer.specific_fields]
            swimmer_data = { key: request.data[key]
                            for key in swimmer_fields
                            if key in request.data }
            swimmer_data['type'] = user.id
            serializer = SwimmerSerializer(data=swimmer_data)

        elif sport == 1:
            runner = RunnerSerializer()
            runner_fields = [key for key in RunnerSerializer.specific_fields]
            runner_data = { key: request.data[key]
                            for key in runner_fields
                            if key in request.data }
            runner_data['type'] = user.id
            serializer = RunnerSerializer(data=runner_data)

        if serializer.is_valid():
            sport_user = serializer.save()
            return Response(success_response(serializer.data),
                            status=status.HTTP_201_CREATED)

        return Response(error_response(serializer.errors),
                        status=status.HTTP_400_BAD_REQUEST)
开发者ID:alvaromlg,项目名称:narwhals,代码行数:57,代码来源:views.py


注:本文中的serializers.UserSerializer.get_fields方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。