from rest_framework import serializers from location.models import Country class CountrySerializer(serializers.ModelSerializer): country_code_2 = serializers.CharField() class Meta: model = Country fields = ( "country_code_2", ) def validate(self, data): data["code"] = self.get_country_code(data) del(data['country_code_2']) return data def create(self, validated_data): # Some countries already in database country, _ = Country.objects.get_or_create(**validated_data) return Country def get_country_code(self, obj): print(f"OBJECT: {obj}") return obj.get("country_code_2")