class UserRegistrationTokenSerializer(serializers.ModelSerializer): user = CustomUserSerializer() registration_token = serializers.ReadOnlyField(source='registration_token') class Meta: model = UserRegistrationToken fields = ('user', 'registration_token')
def _ok(self): User = get_user_model() user_1 = User.objects.create_user( email='*****@*****.**', password='******', first_name='John', last_name = 'Smith', hometown='Moscow', bio = 'Im some user this portal' ) user_2 = User.objects.create_user( email='*****@*****.**', password='******', first_name='Jan', last_name = 'Pere', hometown='Paris', bio = 'Im a human' ) data = CustomUserSerializer(user_1).data expected_data = [ { 'id': user_1.id, 'password': user_1.password, 'first_name': 'John', 'last_name': 'Smith', 'hometown': 'Moscow', 'bio': 'Im some user this portal' } ]
class PostDetailSerializer(serializers.ModelSerializer): category = CategorySerializer() author = CustomUserSerializer() class Meta: model = Post fields = [ 'id', 'title', 'summary', 'html_post', 'category', 'created_at', 'author', 'just_users', 'main_image' ]
class ChatMembershipSerializer(serializers.HyperlinkedModelSerializer): chat = serializers.SerializerMethodField() other_user = CustomUserSerializer() class Meta: model = ChatMembership fields = ('chat', 'other_user') # We're using this instead of ChatSerializer() so that we can pass the context (which contains the current user) # down to ChatSerializer, and then down to MessageSerializer (which uses it for its 'from_current_user' field) def get_chat(self, obj): serializer = ChatSerializer(instance=obj.chat, context=self.context) return serializer.data
class SentFriendRequestSerializer(serializers.ModelSerializer): to_user = CustomUserSerializer() class Meta: model = FriendshipRequest fields = ('to_user',) def to_representation(self, obj): """Move fields from profile to user representation.""" representation = super().to_representation(obj) user_representation = representation.pop('to_user') for key in user_representation: representation[key] = user_representation[key] return representation
def _get(self): User = get_user_model() user_1 = User.objects.create_user(email='*****@*****.**', password='******', first_name='John', last_name='Smith', hometown='Moscow', bio='Im some user this portal') user_2 = User.objects.create_user(email='*****@*****.**', password='******', first_name='Jan', last_name='Pere', hometown='Paris', bio='Im a human') url = reverse('users-list') response = self.client.get(url) serializer_data = CustomUserSerializer([user_1, user_2], many=True).data self.assertEqual(status.HTTP_200_OK, response.status_code)
def list(self, request, *args, **kwargs): serialized_data = CustomUserSerializer(self.queryset, many=True).data return Response({"ok": "true", "members": serialized_data})
def likes(self, request, pk=None): recommendation_list = self.queryset.get(id=pk) users = recommendation_list.likes.all() serializer = CustomUserSerializer(users, many=True) return Response(serializer.data)
class PostedSongSerializerForView(serializers.ModelSerializer): user_id=CustomUserSerializer() class Meta: model = PostedSong fields = ('song_id','user_id','song_title','is_public','genre','tag','audio_file')
class TokenSerializer(serializers.ModelSerializer): user = CustomUserSerializer() class Meta: model = Token fields = ('key', 'user')
def update_user(user, request): serializer = CustomUserSerializer(user, data=request.data, partial=True) if serializer.is_valid(raise_exception=True): return serializer.save()
def create_user(request): serializer = CustomUserSerializer(data=request.data) if serializer.is_valid(raise_exception=True): return serializer.save()