google maps - Django (Geo) Geocode - geocode on model save not working -
i've got save override on model:
def save(self, **kwargs): if not self.location: address = u'%s %s %s %s' % (self.address_first, self.address_second, self.address_third, self.postcode) geocoder = nominatim() try: _, latlon = geocoder.geocode(address) except (geopyerror, valueerror): pass else: latlon = geocoder.geocode(address) point = 'point({0} {1})'.format(latlon[1][1], latlon[1][0]) self.location = geos.fromstr(point) super(venue, self).save()
i getting
typeerror: 'nonetype' object not iterable
specifically on address variable. works fine if hardcode address in.
i assume it's fact self.address... not available until after model has saved? what's best way this? save twice?
thanks
updated in case else has issue. google expecting api requests have address in such format line1+line2+line3+zipcode, rather line1, line2, line, zipcode etc etc. format string before try , geocode. it's not django/python specific.
def save(self, **kwargs): if not self.location: address = u'{0}, {1}, {2}'.format(self.address_second, self.address_third, self.postcode) address = address.replace(" ", "+").replace(',', '') geocoder = googlev3() try: _, latlon = geocoder.geocode(address) except (geopyerror, valueerror): pass else: latlon = geocoder.geocode(address) point = 'point({0} {1})'.format(latlon[1][1], latlon[1][0]) self.location = geos.fromstr(point) super(venue, self).save()