简体   繁体   English

使用google appengine在django-nonrel中保存实体

[英]Saving entities in django-nonrel with google appengine

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). 更新:我注意到当我使用视图(和create_object函数)保存实体时,实体被保存(并在数据存储区查看器中可用)。 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()). 但是当我使用shell(manage.py shell)来创建和保存新实体时,它不会被提交到存储(但仍可以在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. 我开始玩带有google appengine的django-nonrel,我对拯救实体这么简单的事感到沮丧。

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: 为此:

  1. I added new django module with models.py: 我用models.py添加了新的django模块:

     from django.db import models class Tes(models.Model): name = models.CharField(max_length=150) 
  2. 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. 但是当我在一分钟后尝试运行它时, Tes.objects.all()什么都不返回。 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. 当我查看http:// localhost:8000 / _ah / admin / datastore时,我只能从select字段中选择AhAdminXrsfToken

Anyway, what am I missing? 无论如何,我错过了什么? Where I can find some kind of logs which would tell me what's wrong? 在哪里我可以找到某种日志,它会告诉我什么是错的?

This is a gotcha that causes a lot of confusion. 这是一个引起很多困惑的陷阱。 From the djangoappengine docs : 来自djangoappengine文档

Also, never run manage.py runserver together with other management commands at the same time. 此外,永远不要同时运行manage.py runserver和其他管理命令。 The changes won't take effect. 更改不会生效。 That's an App Engine SDK limitation which might get fixed in a later release. 这是App Engine SDK的限制,可能会在以后的版本中得到修复。

So you can't do manage.py runserver and manage.py shell at the same time. 所以你不能同时做manage.py runservermanage.py shell 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. App Engine SDK强制执行本地数据存储区锁定。 Make sure you have stopped the server before you start the shell. 确保在启动shell之前已停止服务器。

Shoudn't it be t.put() if you are creating an entity rather than saving it? 如果你要创建一个实体而不是保存它,那么它不是t.put()吗? I use put() to create an entity and it works for me. 我使用put()创建一个实体,它适用于我。 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. 如果你导入django,你可能想知道django还有其他选择,例如我选择GAE + Jinja2 + WTForms,特别是现在不推荐使用google.db.djangoforms选择表单框架,模板引擎和db框架而且您不必导入django,这通常会导致您导入的内容远远超出您的需要。

So my recommendation is to avoid import django... and instead use Jinja2 + WTForms. 所以我的建议是避免import django...而是使用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. 如果你真的想在app引擎上使用django那么你可能想要检查项目www.allbuttonspressed.com,该项目启用所有django for google app引擎但是当我怀疑我们需要的是模板引擎时,请确保你需要这么多django一个表单框架,我们可以不用django。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM