class ExportDestinationSerializer(serializers.HyperlinkedModelSerializer): """ Serializer class for the :class:`~accounts.models.export_destination.ExportDestination` model. """ users = UserSerializer(many=True, read_only=True) user_ids = serializers.PrimaryKeyRelatedField( write_only=True, required=False, allow_null=True, source="users", label="User IDs", help_text="Sets users access using primary keys.", many=True, queryset=User.objects.all(), ) class Meta: model = ExportDestination fields = ( "id", "title", "description", "ip", "username", "password", "destination", "users", "user_ids", "port", "socket_timeout", "negotiation_timeout", "banner_timeout", )
def post(self, request): serializer = UserSerializerCreate(data=request.data, context={'request': request}) if serializer.is_valid(): user = serializer.save() user.set_password(serializer.validated_data['password']) user.save() Profile(user=user).save() return Response(UserSerializer(user).data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def user_create(request): serialized = UserSerializer(data=request.data) if serialized.is_valid(): serialized.save() return Response( { 'success': True, 'msg': 'Usuário criado com sucesso.', }, status=status.HTTP_201_CREATED ) else: return Response( { 'success': False, 'msg': serialized.errors, }, status=status.HTTP_400_BAD_REQUEST )
class PostSerializer(serializers.ModelSerializer): post_reply_count = serializers.SerializerMethodField() tracks = PostReplySerializer(many=True, read_only=True) user = UserSerializer() class Meta: model = Post fields = '__all__' def get_post_reply_count(self, post): return PostReply.objects.filter(post=post).count()
def current_user(request): if not request.auth: return Response( status=status.HTTP_401_UNAUTHORIZED ) return Response( { 'success': True, 'msg': UserSerializer(request.user).data, }, status=status.HTTP_200_OK )
class ProfileSerializer(serializers.HyperlinkedModelSerializer): """ `Serializer <https://www.django-rest-framework.org/api-guide/serializers/>`_ class for the :class:`~accounts.models.profile.Profile` model. """ url = serializers.HyperlinkedIdentityField( view_name="accounts:profile-detail") user = UserSerializer() class Meta: model = Profile fields = "__all__"
def setUp(self): self.user_attributes = { "first_name": "Sprint", "last_name": "Fast", "email": "*****@*****.**", } self.serialized_data = { "first_name": "Sprint", "last_name": "Fast", "email": "*****@*****.**", } self.user = UserModel.objects.create(**self.user_attributes) self.serializer = UserSerializer(instance=self.user)
def get(self, request): users = User.objects.all() serializer = UserSerializer(users, many=True) return Response(serializer.data)
class AdministratorSerializer(serializers.ModelSerializer): user = UserSerializer() class Meta: model = Administrator fields = '__all__'