gpt4 book ai didi

python - GAE : Model loses track of parent->child relationship

转载 作者:搜寻专家 更新时间:2023-10-30 20:31:16 24 4
gpt4 key购买 nike

我在 google app engine 数据存储中遇到了一个实体关系似乎非常奇怪的问题。我正在开发一个 Python/GAE 网络应用程序(学习练习),完整的代码可以在 sourceforge 上找到.

  • 我有 2 个模型:
    • Gallery - 搜索词和(间接)照片列表
    • Photo - 有关照片的信息,以及它所属的画廊 (collection_index='photos')
  • 我有一个创建图库并向其中添加照片的摄取过程
  • 我有一个页面,它从数据存储中读取图库并使用该图库实例的“.photos”属性来获取其中的照片列表

现在这就是奇怪的部分出现的地方...... 如果我更改文件(我测试过的任何文件)或者甚至只是更新文件的时间戳(即,它会被重新加载)。 .. 画廊的“.photos”属性开始失败。 例如,如果我尝试加载 page for the "flowers" gallery :

Traceback (most recent call last):
File "C:\Applications\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 700, in __call__ handler.get(*groups)
File "C:\Eclipse-Workspace\galleries-google\app\views\gallery.py", line 33, in get for photo in gallery.photos:
AttributeError: 'Gallery' object has no attribute 'photos'

我可以重新启动 webapp,我可以重新启动应用程序启动器,然后再启动 webapp。尽管如此,问题仍然存在。看来我需要强制数据存储以某种方式“记住”连接

# Reading the list of photos for a given gallery via the Photo entity
# This seems to force the datastore to "remember" the connection between the two
from google.appengine.ext import db
import pprint
from app.models.gallery import Gallery
from app.models.photo import Photo

gallery = Gallery.get_by_key_name('candy')
print("Gallery: " + gallery.search_term)
q = db.GqlQuery("SELECT * FROM Photo WHERE gallery = :1", gallery )
photos = q.fetch(20)

for photo in photos :
print("\tphoto: " + photo.getUrl('original'))

或者通过从头开始重新摄取所有数据(尽管我想即使只是重新摄取一个画廊也可以)。

有没有人知道是什么原因造成的?任何帮助都将不胜感激。

注意事项:

  • 这是在开发环境中。我还没有达到值得在真实服务器上将其注册为 Web 应用程序的程度。
  • 奇怪的是,我最近问了一个关于如何 How to list child nodes in parent 的问题在 GAE 中...因为我真的希望信息存在于父模型定义中。具有讽刺意味的是,(显然) parent 缺乏这些信息导致了现在的问题

最佳答案

所以,经过大量挖掘,我相信我找到了答案......

  • 当文件被修改(时间戳改变)时,应用引擎使它拥有的已编译/缓存文件无效
  • Photo定义了类,它向 Gallery<code> based on the collection of ids it has for them</code> 添加了一个属性
  • <code>
    <li>When it needs the <code>Gallery</code> class, the app engine reloads the code for it... because it's definition was invalidated by a file being modified</li>
    <li>Since it doesn't know it needs the <code>Photo</code> class, it doesn't load that file, so the "new instance" of the <code>Gallery</code> class doesn't have the property that should be created for it's collection</li>
    </code>
<code>

<p>My first solution was to add a dependency on the <code>Photo</code> file in the <code>Gallery</code> file. </p>

</code><pre><code>from app.models.photo import Photo
</code></pre>

<p>The <em>problem</em> with this is that it creates a circular dependency between the two files (since Photo already needs to know about Gallery in order to have a list of them). While it worked, I was told that Python has "problems" with circular dependencies, and it's probably not a good thing to do; it may come back to bite me later.</p>

<strong>The solution I wound up going with was to have the __init.py</strong>相关包的 文件包括两个文件的依赖项。因此,任何时候需要其中一个文件时,都会加载这两个文件。

关于python - GAE : Model loses track of parent->child relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com