furious-luke / django-address

A Django address model and field. Addresses may be specified by address components or by performing an automatic Google Maps lookup.
BSD 3-Clause "New" or "Revised" License
431 stars 181 forks source link

Support for model serializer with django-rest-framework #185

Open kiraware opened 1 year ago

kiraware commented 1 year ago

Hello. Please add support to serialize model to support REST API in Django. Something like django-phonenumber-field package. It would be very nice if it has the ability to serialize data out of the box without implement it manually to API view endpoint

banagale commented 1 year ago

Thanks for the request.

Here's a direct link to the PhoneNumberField serializer field docs

kiraware commented 1 year ago

Thanks for your response!

kiraware commented 1 year ago

Also i wanna suggest that AddressField model look something like openid connect address claim, may have a look at https://openid.net/specs/openid-connect-core-1_0.html#AddressClaim Thanks again @banagale

odin1in commented 1 year ago

I use this monky patch for my project.

class AddressField(serializers.CharField):

    def to_representation(self, instance):
        data = {}
        data['raw'] = instance.raw 
        data['street_number'] = instance.street_number 
        data['route'] = instance.route 
        data['latitude'] = instance.latitude 
        data['longitude'] = instance.longitude

        if instance.locality:
            data['locality'] = instance.locality.name
            data['postal_code'] = instance.locality.postal_code
            if instance.locality.state:
                data['state'] = instance.locality.state.name
                data['state_code'] = instance.locality.state.code
                if instance.locality.state.country:
                    data['country'] = instance.locality.state.country.name
                    data['country_code'] = instance.locality.state.country.code

        return data

    def to_internal_value(self, data):
        return parse_address(data)