本文整理匯總了Python中users.serializers.UserSerializer類的典型用法代碼示例。如果您正苦於以下問題:Python UserSerializer類的具體用法?Python UserSerializer怎麽用?Python UserSerializer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了UserSerializer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: post
def post(self, request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
new_user = serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例2: post
def post(self, request, format=None):
request_data = request.data
client_data = request_data["client"]
try:
client = Client.objects.get(user__email=client_data["user"]["email"])
user_serializer = UserSerializer(client.user, data=client_data["user"])
except Client.DoesNotExist:
client = None
user_serializer = UserSerializer(data=client_data["user"])
# Update information for user.
if user_serializer.is_valid():
user_serializer.save()
else:
return Response(user_serializer.errors)
del client_data["user"]
client_data["user_id"] = user_serializer.instance.id
# Update information for client.
if client:
serializer = ClientSerializer(client, data=client_data)
else:
serializer = ClientSerializer(data=client_data)
if serializer.is_valid():
serializer.save()
# Add this client to the provision table.
users_db_utils.add_to_client_provision(
serializer.instance.id)
return Response(serializer.data,
status=status.HTTP_201_CREATED)
return Response(serializer.errors)
示例3: put
def put(self, request, format=None):
my_profile = request.user.get_profile()
# User should be able to edit first_name, last_name, email here
# TODO: Make sure this is safe as we are making two calls to serialize the request
user_serializer = UserSerializer(
request.user,
data=request.DATA,
partial=True
)
if not user_serializer.is_valid():
return errors.New400BadRequest(user_serializer.errors)
user_serializer.save()
serializer = ProfileSerializer(
my_profile,
data=request.DATA,
partial=True
)
print serializer.errors
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return errors.New400BadRequest(serializer.errors)
示例4: create
def create(self, req):
serializer = UserSerializer(data=req.data, context={'request': req})
if serializer.is_valid():
new_user = serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例5: post
def post(self, request):
serializer = UserSerializer(data=request.data) #en rest framework los datos que se envían están en data, y no en POST
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=HTTP_201_CREATED)
else:
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
示例6: put
def put(self, request, userid):
user = self.get_user(userid)
serializer = UserSerializer(user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例7: create
def create(self, request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
new_user = serializer.save()
send_mail(u"Bienvenido!", u"Bienvenido a Frikr!", "[email protected]", [new_user.email], fail_silently=True)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例8: UpdateUserTest
class UpdateUserTest(APITestCase):
def setUp(self):
self.user = User.objects.create(username="jake", first_name="Jake")
self.data = UserSerializer(self.user).data
self.data.update({'first_name': 'Changed'})
def test_can_update_user(self):
response = self.client.put(reverse('user-detail', args=[self.user.id]), self.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
示例9: create
def create(self, request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
new_user = serializer.save()
# Devolvemos los datos después de crear el recurso, es como un acuse de recibo.
# no todas las apis devuelven los datos.
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例10: update
def update(self, request, pk):
user = get_object_or_404(User, pk=pk)
# Paso al serializados que instancia y con que la tiene que actualizar.
serializer = UserSerializer(instance=user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例11: put
def put(self, request, pk):
user = get_object_or_404(User, pk=pk)
self.check_object_permissions(request, user)
serializer = UserSerializer(User, request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.data, status=status.HTTP_400_BAD_REQUEST)
示例12: update
def update(self, request, pk):
user = get_object_or_404(User, pk=pk)
self.check_object_permissions(request, user) # compruebo si el usuario autenticado puede hacer PUT en este user
serializer = UserSerializer(instance=user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例13: put
def put(self, request, pk):
user = get_object_or_404(User, pk=pk)
self.check_object_permissions(request, user) # llama a has_object_permission
serializer = UserSerializer(user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=HTTP_202_ACCEPTED)
else:
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
示例14: update
def update(self, req, pk):
user = get_object_or_404(User, pk=pk)
self.check_object_permissions(req, user) # permisos a mano
serializer = UserSerializer(instance=user, data=req.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
示例15: update
def update(self, request, pk):
self.check_permissions(request)
user = get_object_or_404(User, pk=pk)
self.check_object_permissions(request, user)
serializer = UserSerializer(instance=user, data=request.data) #Serializa este usuario con data datos
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)