Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

hemamaps / django-countries   python

Repository URL to install this package:

Version: 3.4.1 

/ serializer_fields.py

from __future__ import unicode_literals

from rest_framework import serializers
from django.utils.encoding import force_text

from django_countries import countries


class CountryField(serializers.Field):

    def __init__(self, *args, **kwargs):
        self.country_dict = kwargs.pop('country_dict', None)
        countries_class = kwargs.pop('countries', None)
        self.countries = countries_class() if countries_class else countries
        super(CountryField, self).__init__(*args, **kwargs)

    def to_representation(self, obj):
        code = self.countries.alpha2(obj)
        if not code:
            return ''
        if not self.country_dict:
            return code
        return {'code': code, 'name': force_text(self.countries.name(obj))}

    def to_internal_value(self, data):
        if isinstance(data, dict):
            data = data.get('code')
        return self.countries.alpha2(data)