#Return post object instead of it's id

3 messages · Page 1 of 1 (latest)

clear bay
#

This is the current return from API

    "detail": {
        "id": 2,
        "nickname": "Vlad",
        "creation_date": 1713985234337,
        "is_superuser": true,
        "is_staff": true,
        "is_active": true,
        "profile_image": "/media/profile_images/eu.jpg",
        "posts": [
            51,
            52,
            58,
            61,
            62,
            69,
            89
        ]
    },
    "status": 200
}```

What i want to achieve is 
```{
    "detail": {
        "id": 2,
        "nickname": "Vlad",
        "creation_date": 1713985234337,
        "is_superuser": true,
        "is_staff": true,
        "is_active": true,
        "profile_image": "/media/profile_images/eu.jpg",
        "posts": [
            {
              title: "ABC"
            },

        ]
    },
    "status": 200
}```

`serializer.py`
```from rest_framework import serializers
from .models import NewUser

from heyllux.models import Post
# from heyllux.serializer import PostSerializer

class UserSerializer(serializers.ModelSerializer):
    posts = serializers.PrimaryKeyRelatedField(many=True, queryset=Post.objects.all())
    
    class Meta:
        model = NewUser
        fields = ['id','nickname', 'creation_date', 'is_superuser', 'is_staff', 'is_active', 'profile_image', 'posts']
        ```
#

Post model

    title = models.CharField(max_length=100, blank=True)
    description = models.TextField(max_length=500, blank=True)
    image = models.ImageField(upload_to='uploads/', blank=True)
    posted_date = models.CharField(blank=True, max_length=15)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)
    likes = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='posts', blank=True)
    total_likes = models.IntegerField(null=True, default=0)
    is_liked = models.BooleanField(default=False, blank=True)
    
    def total_likes(self):
        return self.likes.count()
    
    def __str__(self):
        return self.title```
#

Got IT