Elisa
Elisa

Reputation: 7093

Can I modify field type in serializer for ModelSerializers?

I have model like:

class Profile(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=20)
    profile_picture = ImageField(upload_to=settings.MEDIA_ROOT, blank=True)

I have serializers:

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile

In the serializers, I want to modify profile_picture field time to something else. Is that possible? how?

Upvotes: 0

Views: 93

Answers (1)

Mark Galloway
Mark Galloway

Reputation: 4151

By specifying the source parameter, you can rename fields from your model to anything you would like.

class ProfileSerializer(serializers.ModelSerializer):
    some_other_name = serializers.ImageField(source='profile_picture')

    class Meta:
        model = Profile
        fields = ('some_other_name',)

Upvotes: 1

Related Questions