2013-02-12 39 views
0

我應該如何理解這一點?以及這會如何發生? 走進Django的外殼:在django模型中保存數據實際上並不節省

>>> from customauth.models import Profile 
>>> p = Profile.objects.get(user_id=1) 
>>> p.status 
u'34566' 
>>> p.status = 'qwerty' 
>>> p.status 
'qwerty' 
>>> p.save() 
>>> p.status 
'qwerty' 
>>> p = Profile.objects.get(user_id=1) 
>>> p.status 
u'qwerty' 
>>> 

退出,再次進入運行Django shell:

>>> from customauth.models import Profile 
>>> p = Profile.objects.get(user_id=1) 
>>> p.status 
u'qwerty' 
>>> 

似乎一切都OK。但現在進入dbshel​​l:

mysql> select user_id, status from customauth_profile where user_id=1; 
+---------+--------+ 
| user_id | status | 
+---------+--------+ 
|  1 | 34566 | 
+0

可能很重要:我使用django.contrib.gis.db.backends.mysql後端數據庫(需要djagno_cities應用程序)和託管在亞馬遜上的項目,其GPL MySQL RDS作爲數據庫。 – 2013-02-12 11:05:48

回答

0

問題出在緩存模塊中。奇怪,但沒有緩存中間件被插入,緩存不用於保存方法。 曾用過django.core.cache.backends.locmem.LocMemCache模塊,可能是它的破或者有一些奇怪的功能,我不知道。將嘗試memcached模塊,它應該(我希望)修復問題,而無需打開現場緩存。

0

如果數據在關閉shell後仍然存在,那麼數據很可能不會保存到數據庫中。你能檢查你的settings.py並確保你保存到正確的mysql數據庫嗎?

+0

設置中只有一個數據庫。它在兩個小時內被保存了兩次,而我試圖解決這個問題。但只有兩次,我不知道爲什麼......每次成功保存時,我都會再次嘗試,但沒有更改代碼,但又失敗了。 – 2013-02-12 11:01:00

0

shell會話是單個數據庫事務。由於事務隔離,外部連接不會看到更改。您需要提交事務 - 最簡單的方法是退出shell並重新啓動。

在正常的請求中,Django在請求結束時自動提交,所以這種行爲不是問題。

+0

我的第二個代碼塊在我的問題 - 重新啓動shell會話。我做了更改,退出shell並重新啓動。並且它表明應用了更改。 – 2013-02-12 17:28:14

相關問題