2013-04-04 142 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() 
相關問題