Django Rest Framework nested or related serializers please

I have end point for a post and the serializer that feeds that endpoint looks like this:

class PostSerializer(serializers.ModelSerializer):
    images = PostImageSerializer(many=True, read_only=True, required=False)
    uploaded_images = serializers.ListField(
        required=False,
        child=serializers.FileField(
            allow_empty_file=False,
            use_url=True,
        ),
        write_only=True,
    )
    user_profile = ProfileSerializer(many=True, read_only=True)

    class Meta:
        model = Post
        fields = [
            "id",
            "category",
            "body",
            "images",
            "uploaded_images",
            "video",
            "can_view",
            "can_comment",
            "user",
            "user_profile",
            "published",
            "pinned",
            "created_at",
            "updated_at",
        ]

    def create(self, validated_data):
        new_post = Post.objects.create(**validated_data)
        images = dict((self.context["request"].FILES).lists()).get("files", None)
        if images:
            for image in images:
                PostImage.objects.create(
                    image=image, post=new_post, user=validated_data["user"]
                )
        return new_post

and the data at the end point looks like this:

	
id	1
category	1
body	"Hello how are you this is a post"
images	
0	
id	1
image	"http://127.0.0.1:8000/storage/posts/post_1/0pxYb.jpg"
post	1
user	1
1	
id	2
image	"http://127.0.0.1:8000/storage/posts/post_1/12729465_237302183268986_1508875389_n.jpg"
post	1
user	1
2	
id	3
image	"http://127.0.0.1:8000/storage/posts/post_1/39342221_884539318417339_7097049967600074752_n.jpg"
post	1
user	1
3	
id	4
image	"http://127.0.0.1:8000/storage/posts/post_1/40024140_307464703336377_7213765493415477248_n.jpg"
post	1
user	1
video	"http://127.0.0.1:8000/storage/user_1/posts/Beer.jpg"
can_view	"Everybody"
can_comment	"Everybody"
user	1
published	true
pinned	false
created_at	"2022-12-11T10:16:31.784169Z"
updated_at	"2022-12-11T10:16:31.784232Z"

The user has a profile and I need to get some of that profile data into this endpoint, but it’s not working for me. How can I get data from user profile into this end point please. I cannot find the answer in the DRF docs of how I can use a field value (user) in a serializer to get related data from another model (profile)

We might need to see your Post model.

Right now, you’re saying that there’s going to be an attr called user_profile on a Post instance.

And that may not be the case. Your Post model most probably contains a user foreign key.
Is the “Profile” a separate model from the User model?
If so, also post the Profile model here.

Yes I get that my approach may be wrong. On a post I only have a field for the user which is a foreign key. The user has a profile and I need to pull that profile data (first_name, last_name, picture) into the post and reply end points so I can show that on the front end.

Here is my post models:

class Post(models.Model):

    EV = "Everybody"
    FO = "Followers"
    FR = "Friends"
    AUDIENCE = [
        (EV, "Everybody"),
        (FO, "Followers"),
        (FR, "Friends"),
    ]
    category = models.ForeignKey(Category, on_delete=models.SET_DEFAULT, default=1)
    slug = AutoSlugField(populate_from=["category", "created_at", "user"])
    body = models.TextField("content", blank=True, null=True, max_length=5000)
    video = models.FileField(upload_to=user_directory_path, null=True, blank=True)
    can_view = models.CharField(max_length=10, choices=AUDIENCE, default=EV)
    can_comment = models.CharField(max_length=10, choices=AUDIENCE, default=EV)
    user = models.ForeignKey(
        User, on_delete=models.CASCADE, verbose_name="author", related_name="author"
    )
    published = models.BooleanField(default=False)
    pinned = models.BooleanField(default=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = "post"
        verbose_name_plural = "posts"
        db_table = "posts"
        ordering = ["created_at"]

    def __str__(self):
        return self.body[0:30]

    def get_absolute_url(self):
        return self.slug


class PostImage(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="images")
    image = models.FileField(
        upload_to=post_directory_path, default="posts/default.png", null=True
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = "post_images"
        ordering = ["post"]

    def image_tag(self):
        return mark_safe(
            '<img src="/storage/%s" width="50" height="50" />' % (self.image)
        )

    image_tag.short_description = "Image"


class Reply(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    video = models.FileField(upload_to=reply_videos_directory_path, null=True, blank=True)
    reply = models.TextField(max_length=256, default=None)
    parent = models.ForeignKey(
        "self", on_delete=models.CASCADE, null=True, blank=True, related_name="replies"
    )

    created_at = models.DateTimeField(auto_now_add=True, verbose_name="Created at")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="Updated at")

    class Meta:
        verbose_name = "post reply"
        verbose_name_plural = "post replies"
        db_table = "post_reply"

    def __str__(self):
        return self.reply[0:30]


class ReplyImage(models.Model):
    reply = models.ForeignKey(Reply, on_delete=models.CASCADE, related_name="images")
    image = models.FileField(
        upload_to=reply_directory_path, default="posts/replies/default.png", null=True
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = "reply_images"
        ordering = ["reply"]

    def image_tag(self):
        return mark_safe(
            '<img src="/storage/%s" width="50" height="50" />' % (self.image)
        )

    image_tag.short_description = "Image"

I have a customer user model and a profile models that references a user. How can I get the user profile data into the post and reply end points please?

Here is the profile model: 

class Profile(models.Model):

# Gender
M = 'M'
F = 'F'
O = 'O'
GENDER = [ 
    (M, "Male"),
    (F, "Female"),
    (O, "Other")
]

# Basic information
background = models.ImageField(upload_to=background_to, null=True, blank=True)
photo = models.ImageField(upload_to=image_to, null=True, blank=True)
slug = AutoSlugField(populate_from=['first_name', 'id', 'gender'])
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100, null=True, blank=True)
last_name = models.CharField(max_length=100)
birthdate = models.DateField()
gender = models.CharField(max_length=6)
bio = models.TextField(max_length=5000, null=True, blank=True)
languages = ArrayField(models.CharField(max_length=30, null=True, blank=True), null=True, blank=True)

# Location information
website = models.URLField(max_length=256, null=True, blank=True)

# author information 
user = models.OneToOneField(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="created at")
updated_at = models.DateTimeField(auto_now=True, verbose_name="updated at")

class Meta:
    verbose_name = "profile"
    verbose_name_plural = "profiles"
    db_table = "user_profiles"

def __str__(self):
    return self.first_name + ' ' + self.last_name

def get_absolute_url(self):
    return self.slug    

def image_tag(self):
    return mark_safe('<img src="/storage/%s" width="50" height="50" />' % (self.photo))

image_tag.short_description = 'Image'

I think that you can use the source attribute on the serializer. Docs

So here, you can define the source attr to the author.profile

Ok thank you I will see how I can use that thanks