本文整理汇总了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)