[Fixed]-Django Rest Framework – mapping serializers fields to a database column name

9👍

Thanks to all. Finally I got it. See code bellow. I answer my question due to there is no option to add large amount of lines on a answer comment.

serializers.py

from rest_framework import serializers
from .models import Country, Region, City


class CountrySerializer(serializers.ModelSerializer):

    class Meta:
        model = Country
        fields = ('id', 'name')


class RegionSerializer(serializers.ModelSerializer):

    class Meta:
        model = Region
        fields = ('id', 'name', 'code')


class CitySerializer(serializers.ModelSerializer):

    country_id = serializers.PrimaryKeyRelatedField(
        queryset=Country.objects.all(),
        required=True,
        source='country',
    )
    region_id = serializers.PrimaryKeyRelatedField(
        queryset=Region.objects.all(),
        allow_null=True,
        required=False,
        source='region',
    )

    country = CountrySerializer(
        read_only=False,
        required=False,
    )
    region = RegionSerializer(
        required=False,
        allow_null=True,
        read_only=True,
    )

    class Meta:
        model = City
        fields = (
            'id',
            'country', 'region',
            'name', 'postal_codes',
            'country_id', 'region_id',
        )

12👍

You can use the source parameter of a field on your serializer to achieve this. For example:

from rest_framework import serializers

from .models import City


class CitySerializer(serializers.ModelSerializer):
    country_id = serializers.IntegerField(source='country')
    region_id = serializers.IntegerField(source='region')

    class Meta:
        model = City
        fields = ('id', 'country_id', 'region_id', 'name', 'postal_codes')

EDIT: As Yaroslav pointed out, when doing it in this way, you don’t need to include the source. Take note, however, that simply including country_id or region_id in the fields list is not sufficient. You still need to specify the field on the serializer, such as country_id = serializers.IntegerField() and also include it in the fields.

0👍

You can also put the rename field to read only but original model only write only like. So that while returning the response, you don’t have two fields one without id and another with suffix _id

class CitySerializer(serializers.ModelSerializer):
    country_id = serializers.IntegerField(source='country', read_only=True)
    region_id = serializers.IntegerField(source='region', read_only=True)

    class Meta:
        model = City
        extra_kwargs = {
           "country": {"write_only": True},
           "region": {"write_only": True}
        }

Leave a comment