本文整理匯總了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)