本文整理匯總了Python中users.serializers.UserSerializer.save方法的典型用法代碼示例。如果您正苦於以下問題:Python UserSerializer.save方法的具體用法?Python UserSerializer.save怎麽用?Python UserSerializer.save使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類users.serializers.UserSerializer
的用法示例。
在下文中一共展示了UserSerializer.save方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: put
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例2: put
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例3: post
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例4: post
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例5: update
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例6: update
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例7: update
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例8: put
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例9: put
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例10: update
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例11: put
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
def put(self, request, pk):
user = get_object_or_404(User, pk=pk)
if request.user.is_superuser or request.user.username == user.username:
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)
else:
return Response(status=status.HTTP_401_UNAUTHORIZED)
示例12: put
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
def put(self, request, pk):
user = get_object_or_404(User, pk=pk) # devuelve el objeto o, si este no existe, un error 404
# compruebo si el usuario autenticado puede hacer PUT en este user
# Hay que hacerlo a mano porque estamos heredando de una GenericAPIView
# Al hacer la recuperación del objeto 'user' manualmente, la comprobacion tambien debe ser manual
self.check_object_permissions(request, 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: create
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例14: post
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
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)
示例15: put
# 需要導入模塊: from users.serializers import UserSerializer [as 別名]
# 或者: from users.serializers.UserSerializer import save [as 別名]
def put(self, request, pk):
"""
Endpoint de modificación de usuario. Por convención, se utiliza la url de listado con una petición PUT para la modificación de un objeto de ese listado. En el serializer.save() comprueba automáticamente si tiene instancia del User; si la tiene, coge esa instancia y llama al update() del serializer; si no la tiene, llama al método create() del serializer, como en el caso del POST del UserListAPI
"""
user = get_object_or_404(User, pk=pk)
# compruebo manualmente si el usuario autenticado puede hacer PUT en este user
self.check_object_permissions(request, user)
# Actualiza los datos de la instancia recuperada con los datos que me pasan por la API
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)