'Django error while serializing image model of child field

I am new to this tech, while working on django project i got some issues when i try to serialize Ticket's account.profile_pic

models.py

class Account(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE
    profile_pic = models.ImageField(upload_to='images/profile_pics/', blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

class Ticket(models.Model):

    author = models.ForeignKey(Account, on_delete=models.CASCADE)
    descr = models.TextField(blank=False, null=False)
    likes = models.ManyToManyField(User)
    

serializers.py

class DetailedTicketSerializer(serializers.ModelSerializer):

    # Error occurs on below line: No file associated with ImageField
    author_profile_pic = serializers.ReadOnlyField(source='author.profile_pic')

    author_username = serializers.ReadOnlyField(source='author.user.username')


    class Meta:

        model = Ticket

        fields = ['id', 'author_profile_pic', 'author_username', 'likes', 'descr']

Anyone knows how do i serialize Account.profile_pic's url???



Solution 1:[1]

serialize the account class. in your ticketserializer call the account serializer. Here an example:

class

HobbySerializer(serializers.ModelSerializer):

    class Meta:
        model = Hobby
        fields = '__all__'

class ProfileSerializer(serializers.ModelSerializer):
    user_hobby = HobbySerializer(many=True)

    class Meta:
        model = Profile
        fields = '__all__'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 saro