2013-02-12 40 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

相关问题