使用 google appengine 在 django-nonrel 中保存实体
更新:我注意到,当我使用视图(和 create_object 函数)保存实体时,它们会被保存(并可在数据存储查看器中使用)。但是当我使用 shell (manage.py shell) 创建和保存新实体时,它不会提交到存储(但仍然可以在 Tes.objects.all() 中看到)。
我开始使用 django-nonrel 和 google appengine 一起玩,我对保存实体这样简单的事情感到沮丧。
我已按照说明中所述设置环境。我设法运行示例应用程序并且运行正常。我想扩展它,以便将我的实体保存到存储中。为此:
我使用 models.py 添加了新的 django 模块:
从 django.db 导入模型 类 Tes(models.Model): 名称 = models.CharField(max_length=150)
我创建了一个脚本来保存一些数据:
导入操作系统 导入系统 sys.path.append("d:\\工作空间\\项目\\") os.environ['DJANGO_SETTINGS_MODULE'] = '设置' 从 testmodule.models 导入 Tes t = Tes(名称=“测试”) t.save() tes = Tes.objects.all() 对于 tes 中的 t: 打印名称
该脚本运行时没有错误。当我一次又一次运行它几次时,它会打印越来越多的“测试”字符串。但是,当我尝试在休息一分钟后运行它时,Tes.objects.all() 没有返回任何内容。在此期间,数据存储文件会更改其大小(但也许这只是某种日志)。当我查看 http://localhost:8000/_ah/admin/datastore 我可以选择仅来自选择字段的 AhAdminXrsfToken。
无论如何,我错过了什么?我在哪里可以找到某种日志来告诉我出了什么问题?
Update: I've noticed that entities are saved (and available at the Datastore Viewer) when I save them using views (and the create_object function). But when I use shell (manage.py shell) to create and save new entity it isn't commited to the storage (but still can be seen in Tes.objects.all()).
I started playing with the django-nonrel with google appengine and I am getting frustrated by thing as simple as saving Entities.
I've set up my environment as described in instruction. I managed to run sample application and it runs ok. I'd like to extend it so it saves my entity to the storage. To do so:
I added new django module with models.py:
from django.db import models class Tes(models.Model): name = models.CharField(max_length=150)
I created a script to save some data:
import os import sys sys.path.append("d:\\workspace\\project\\") os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from testmodule.models import Tes t = Tes(name="test") t.save() tes = Tes.objects.all() for t in tes: print t.name
The script runs without errrors. When I run it few times one after another, it prints more and more "test" strings. But when I try running it after a minute break the Tes.objects.all() returns nothing. During that time the datastore file changes it size (but maybe that's just some kind of logs). When I look at the http://localhost:8000/_ah/admin/datastore I can select only AhAdminXrsfToken from select field.
Anyway, what am I missing? Where I can find some kind of logs which would tell me what's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个引起很多混乱的问题。来自 djangoappengine 文档:
所以你不能同时执行
manage.py runserver
和manage.py shell
。如果这样做,对其中一个数据存储的更改将不会在另一个中可见。 App Engine SDK 对本地数据存储区实施了锁定。确保在启动 shell 之前已停止服务器。This is a gotcha that causes a lot of confusion. From the djangoappengine docs:
So you can't do
manage.py runserver
andmanage.py shell
at the same time. If you do, changes to the datastore in one will not be visible in the other. There's a lock on the local datastore enforced by the App Engine SDK. Make sure you have stopped the server before you start the shell.如果您要创建一个实体而不是保存它,不应该是
t.put()
吗?我使用 put() 创建一个实体,它对我有用。如果您导入 django,您可能想知道 django 有其他选择,例如我选择 GAE + Jinja2 + WTForms,特别是现在 google.db.djangoforms 已弃用,为表单选择表单框架、模板引擎,也许还有数据库框架而且您不必导入 django,这通常会导致您导入超出您需要的内容。所以我的建议是避免
import django...
而是使用 Jinja2 + WTForms。如果你真的想在应用程序引擎上使用 django,那么你可能需要签入项目 www.allbuttonspressed.com,它为 google 应用程序引擎启用了所有 django,但当我怀疑我们需要的只是一个模板引擎时,请确保你需要这么多 django,并且一个表单框架,我们可以不用 django。Shoudn't it be
t.put()
if you are creating an entity rather than saving it? I useput()
to create an entity and it works for me. And if you import django you may want to know that there are alternatives to django such as my choice GAE + Jinja2 + WTForms especially now that google.db.djangoforms is deprecated selecting a form framework for forms, a templating engine and perhaps a db framework and you do't have to import django which often results in forcing you to import much more than you need.So my recommendation is to avoid
import django...
and instead use Jinja2 + WTForms. If you really want django on app engine then you might want to check in the project www.allbuttonspressed.com that enables all django for google app engine but be certain that you need this much django when I suspect all we need is a template engine and a form framework and we can do without django.