【发布时间】:2012-07-12 17:03:35
【问题描述】:
我在我的一个模型上覆盖了 save 方法:
def save(self, *args, **kwargs):
self.set_coords()
super(Post, self).save(*args, **kwargs)
def __unicode__(self):
return self.address
# set coordinates
def set_coords(self):
toFind = self.address + ', ' + self.city + ', ' + \
self.province + ', ' + self.postal
(place, location) = g.geocode(toFind)
self.lat = location[0]
self.lng = location[1]
但是,我只想在创建帖子时运行一次set_coords()。更新模型时不应运行此函数。
我怎样才能做到这一点?有什么方法可以检测模型是否正在创建或更新?
【问题讨论】:
标签: django django-1.4