Skip to content

Feature request: option in Meta to rename fields in ModelSerializer #8994

Discussion options

You must be logged in to vote

I've smacked into this one today and the fix is actually pretty straightforward and django-esque.

Set a source attribute and stick to the regular field serializers wherever you can. Using ModelField should probably be an exception not the rule.

Given the posted example:

from django.db import models

class Connection(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    user = organisation = models.ForeignKey(models.CASCADE)

Then a corresponding serializer that does what the OP wants would be:

from rest_framework import serializers

class OrganisationSerializer(serializers.HyperlinkedModelSerializer):
    creation_date = serializers.DateTimeField(source="date")

    class Meta

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@christophehenry
Comment options

@auvipy
Comment options

auvipy May 29, 2023
Collaborator

@christophehenry
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by christophehenry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants