2013-04-04 102 views
-1

我已经为UserProfile模型声明了一个信号,该模型更新了其他一些字段。存储的数据来自Web服务。保存期间更新其他字段

post_save.connect(user_profile_update, sender=UserProfile) 

在user_profile_update,我这样做:

profile = get_object_or_404(UserProfile, user=instance) 
profile.province = xml.duzeltilmisil #this comes from a web service 
profile.save() 

,我得到这个错误:

'NoneType' object is not callable 
profile.save() 

有一个其他错误,但我做了什么也递归。当我更新UserProfile时,它应该再次触发user_profile_update。

在保存过程中是否有任何合理的方式更新这些字段?

+0

禁用信号,并查看是否仍然出现错误 – karthikr 2013-04-04 21:37:32

+0

karthikr,用户输入的邮寄地址作为一个正常的文本,web服务解析它,它给了我地址的城市,地区等信息,我想在更新后立即保存这些信息。所以我想我需要这个信号。如果我将禁用信号,我不会得到任何错误,但地址不会被解析。 – cem 2013-04-04 21:39:51

+0

为什么你需要一个信号来解析?您可以在视图中更好地处理它。信号只适用于较小的更新,不适用于执行逻辑 – karthikr 2013-04-04 21:42:04

回答

0

由于您正在解析地址,因此在视图中处理解析是一种更好的方法,而不是信号。

信号正常用于对其他型号的微小更新。 。

所以,你的代码可以是:

profile = get_object_or_404(UserProfile, user=instance) 
profile.province = xml.duzeltilmisil #this comes from a web service 

//parse the address here, and then save the models 
profile.save() 
相关问题