0

Is there any way to display the name of the user in the "likedBy" section of the view, instead of the user id? Using django rest framework From view I get , ignore comments:

[
    {
        "id": 3,
        "title": "ddsa",
        "content": "dsadsa",
        "created": "2021-02-10T08:07:42.758400Z",
        "updated": "2021-02-10T08:07:42.758400Z",
        "author": 1,
        "category": [
            {
                "pk": 1,
                "name": "Life"
            }
        ],
        "likedBy": [
            1
        ],
        "comments": [
            {
                "id": 2,
                "content": "ghfa",
                "created": "2021-02-10T08:08:02.407950Z",
                "post": 3,
                "author": 1,
                "likedBy": [
                    1
                ]
            }
        ]
    }
]

Views.py:

class PostViewSet(FlexFieldsMixin, generics.ListAPIView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permit_list_expands = ['category', 'comments']

Models.py


class Post(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    category = models.ManyToManyField(Category, related_name='posts')
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    likedBy = models.ManyToManyField(User, related_name='posts', blank=True)

    class Meta:
        ordering = ['-created']

Serializers.py:


class PostSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Post
        fields = '__all__'
        expandable_fields = {
            'category': ('blogApi.CategorySerializer', {'many': True}),
            'comments': ('blogApi.CommentSerializer', {'many': True}),
        }

How serialize ManyToMany field to display text values

404pio
  • 1,080
  • 1
  • 12
  • 32
kica121
  • 17
  • 2
  • possibly duplicate https://stackoverflow.com/questions/17280007/retrieving-a-foreign-key-value-with-django-rest-framework-serializers – rahul.m Feb 10 '21 at 10:52

3 Answers3

1

Given that you are not serializing a relation, but rather an attribute of your model which is related to your user, I believe you have to use a serializer.SerializerMethodField(). This allows you to do the following:

class PostSerializer(FlexFieldsModelSerializer):
    liked_by = serializers.SerializerMethodField(method_name="get_user_likes")

    class Meta:
        model = Post
        fields = (
            "title",
            "content",
            "category",
            "author",
            "created",
            "update",
            "liked_by"
        )
        expandable_fields = {
            'category': ('blogApi.CategorySerializer', {'many': True}),
            'comments': ('blogApi.CommentSerializer', {'many': True}),
        }

    @classmethod
    def get_user_likes(obj):
       # do whatever you like here, but I tend to call a 
       # method on my model to keep my serializer file
       # nice and tidy
       # you'll need to define a UserSerializer
       return UserSerializer(obj.get_user_likes(), many=True, read_only=True)

And in your Post model:

class Post(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    category = models.ManyToManyField(Category, related_name='posts')
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    likedBy = models.ManyToManyField(User, related_name='posts', blank=True)

    class Meta:
        ordering = ['-created']

    def get_user_likes(self):
        return self.likedBy.all()

You can of course define the full method in your serializer, but as I said I like to keep the serializer as clean as possible and put all methods associated with models in my models.py.

    @classmethod
    def get_user_likes(obj):
       # you'll need to define a UserSerializer
       return UserSerializer(obj.likedBy.all(), many=True, read_only=True)

anowlinorbit
  • 347
  • 1
  • 2
  • 10
0

So you can set

likedBy = serializers.ReadOnlyField(source='get_likedBy')

in your PostSerializer class and define function in your Post model class like below:

@property
def get_likedBy(self):
   liked_by = []
   for user in self.users_liked_post.all():
       liked_by.append(user.name)
   return liked_by

just use correct related_name instead of users_liked_post

0

If you add likedBy to your expandable fields, and then add ?expand=likedBy to your url, it should give you all the information that you outline in the UserSerializer, or write a new serializer named LikedBySerializer. Also as a general rule, try not to use '__all__' it's a good way to leak data. Happy coding!

class LikedBySerializer(FlexFieldsModelSerializer):
    class Meta:
        model = User
        fields = '__all__'


class PostSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Post
        fields = '__all__'
        expandable_fields = {
            'category': ('blogApi.CategorySerializer', {'many': True}),
            'comments': ('blogApi.CommentSerializer', {'many': True}),
            'likedBy': ('blogApi.LikedBySerializer', {'many': True}),
        }
Paul Tuckett
  • 1,023
  • 11
  • 14