33👍
✅
def save(self, *args, **kwargs):
if not self.pk:
self.set_coords()
super(Post, self).save(*args, **kwargs)
👤jagm
3👍
I think the correct way to do it is using post_save signal:
def set_coords(sender, **kw):
model_instance = kw["instance"]
if kw["created"]:
toFind = model_instance.address + ', ' + model_instance.city + ', ' + \
model_instance.province + ', ' + model_instance.postal
(place, location) = g.geocode(toFind)
model_instance.lat = location[0]
model_instance.lng = location[1]
model_instance.save()
post_save.connect(set_coords, sender=MyModel)
- Django RestFramework group by
- ValueError: "needs to have a value for field "id" before this many-to-many relationship can be used"
- Translating text blocks with Django .. what to do with the HTML?
- Django logging – django.request logger and extra context
Source:stackexchange.com