Home > Software engineering >  How to get some of the UserModel fields into another Serializer
How to get some of the UserModel fields into another Serializer

Time:05-17

I have a model name Comment as follows.

class Comment(models.Model):
    message = models.CharField(max_length=1000)
    time = models.DateTimeField(auto_now_add=True)
    sender = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='usercomment')

    def __str__(self):
        return self.sender.name

For this model, I have a serializer

class CommentSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    time = serializers.DateTimeField(read_only=True)
    message = serializers.CharField(read_only=True)
    
    sender = serializers.PrimaryKeyRelatedField(read_only=True, many=False)
    name = serializers.StringRelatedField(
        source='sender', read_only=True, many=False)

I want to obtain avatar field from use model so I'm going with a new sterilizer to nest into comments

class SenderInformation(serializers.Serializer):
    avatar = serializers.ImageField(read_only=True)

And the main objective is to bring an avatar field linked to the sender, as I'm already getting the user id in send field and name as well but any other way to get a user avatar instead of nesting the few fields from the used model. ? How can I achieve this?

CodePudding user response:

Try StringRelatedField. You are already using it to get the name of the sender, which is being extracted from the user model. So to get an avatar, you can do this:

avatar = serializers.StringRelatedField(source='sender.avatar', read_only=True, many=False)

So the full CommentSerializer will look like this.

class CommentSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    time = serializers.DateTimeField(read_only=True)
    message = serializers.CharField(read_only=True)
    sender = serializers.PrimaryKeyRelatedField(read_only=True, many=False)
    avatar = serializers.StringRelatedField(
        source='sender.avatar', read_only=True, many=False)
    name = serializers.StringRelatedField(
        source='sender', read_only=True, many=False)

You can learn by reading the docs as well.

But there's is one issue with that, it will return the path as a string. So You should use simple ImageField for this purpose.

avatar = serializers.ImageField(source='sender.avatar', read_only=True )

That should work Perfect.

CodePudding user response:

Add a ImageField in your model:

class Comment(models.Model):
    message = models.CharField(max_length=1000)
    time = models.DateTimeField(auto_now_add=True),
    avatar = models.ImageField(),
    sender = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='usercomment')
    def __str__(self):
        return self.sender.name

In your forms.py

from django import forms

class CommentForm(forms.Form):

    message = forms.CharField(max_length=1000)
    time = forms.DateTimeField(auto_now_add=True),
    avatar = forms.ImageField()

You call it in your view as:

f = CommentForm(request.GET)
  • Related