M
M
maxclax2014-12-22 04:00:35
Django
maxclax, 2014-12-22 04:00:35

How to override variable name in Django REST?

I have the following serializers:

class RatesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Rates
        fields = ('from_name', 'to_name', 'drate')

from_name and others is the name of the fields from the model directly. I need them to be displayed under other names in the API itself. For example the same from_name under from. This can be solved by multiplying methods in the model itself. But just because of the name, it's redundant. Is there another way to customize the names for the output?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cu3bluKekc, 2014-12-22
@maxclax

don't like it too?

class RatesSerializer(serializers.ModelSerializer):
    _from = serializers.CharField(source='from_name')
    class Meta:
        model = Rates
        fields = ('_from', 'to_name', 'drate')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question