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

Address Form field only populates if full address is provided. #183

Open AustinGilkison opened 1 year ago

AustinGilkison commented 1 year ago

I am noticing an issue where the Address form field will only populate if the Address Instance of the AddressField has a full address populated. This issue seems to be originating from the to_dict method on the Address model.

In my instance, I am only populating the Country. I assume this means I do not have a Locality or State causing the country part of the below logic to not run.

def as_dict(self):
        ad = dict(
            street_number=self.street_number,
            route=self.route,
            raw=self.raw,
            formatted=self.formatted,
            latitude=self.latitude if self.latitude else "",
            longitude=self.longitude if self.longitude else "",
        )
        if self.locality:
            ad["locality"] = self.locality.name
            ad["postal_code"] = self.locality.postal_code
            if self.locality.state:
                ad["state"] = self.locality.state.name
                ad["state_code"] = self.locality.state.code
                if self.locality.state.country:
                    ad["country"] = self.locality.state.country.name
                    ad["country_code"] = self.locality.state.country.code
        return ad