Hello.. I too needed to do this. I solved so:

1. I've create a field with type SerializerMethodField()

https://github.com/Marlysson/finhis/blob/master/core/serializers.py#L42


2. I created a method that return the attribute that I would like show with 
other name:

https://github.com/Marlysson/finhis/blob/master/core/serializers.py#L46


3. I defined a dictionary named extra_kwargs ( that add some others 
attributes related the fields ) , and the field's model I show them in api 
to send a post to it, and the method field I show in api ( in read mode )

https://github.com/Marlysson/finhis/blob/master/core/serializers.py#L

55

To me this seems a right way to solve this problem.

PS: Put two attributes in fields tuple, I still think that is a redundancy.

Em segunda-feira, 25 de setembro de 2017 15:07:13 UTC-3, Glenn Barney 
escreveu:
>
> So Im new to this and I am writing an api that takes a request with a 
> field like this:
>
> { "content": "Some content" }
>
>
> And I want to save this in  Story model in the db named "content" *but* i 
> want it returned in the API with field post_content like so:
>
> { "post_content": "Some content" }
>
>
> Here is my (stripped down) code:
>
> class StorySerializer(serializers.ModelSerializer):
>
>     post_content = serializers.CharField(source='content', required=False)
>
> class Meta:
>     model = Story
>     fields = ('post_content')
>
> class Story(models.Model):
>     created = models.DateTimeField(auto_now_add=True)
>     content = models.TextField()
>
>     class Meta:
>         ordering = ('created',)
>
>
> How do transform the "content" variable to "post_content"? I need it to be 
> called post_content in the serializer for the data on the way out.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to