如何在(Python)Google App Engine数据源上正确执行一对多连接?

时间:2021-11-09 07:33:48

I have some models set up like:

我有一些模型设置如下:

class Apps(db.Model):
    name        = db.StringProperty(multiline=False)
    description = db.TextProperty()

class AppScreenshots(db.Model):
    image_file     = db.StringProperty(multiline=False)
    description    = db.StringProperty(multiline=False)
    app            = db.ReferenceProperty(Apps)

I'm trying to reference a "parent" app in a screenshot like so:

我试图在屏幕截图中引用“父”应用程序,如下所示:

a = Apps.get(app_key)   
ss = AppScreenshots(
    image_file     = 'foo',
    description    = 'bar',
    app            = a
)
ss.put()

But it complains to me saying:

但它抱怨我说:

BadArgumentError('_app should be a string; received ag1raWxsZXItcm9ib3RzcgoLEgRBcHBzGAkM (a Key):',)

I've tried going over a few examples on the internet and they all seem to work JUST like the above. One set of documentation Google has up suggests doing it a little differently, like this:

我试过在互联网上查看一些例子,他们似乎都像上面那样工作。谷歌提出的一套文件表明这样做有点不同,如下所示:

a = Apps.get(app_key)   
ss = AppScreenshots(
    image_file     = 'foo',
    description    = 'bar',
    app            = a.key()
)
ss.put()

But that gives me the exact same error.

但这给了我完全相同的错误。

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


The problem I found when trying to run your code was that apparently you need to change the name of 'app' in AppScreenshots to something else such as 'apps'. The word 'app' must be reserved in this context.

我在尝试运行代码时遇到的问题是,显然您需要将AppScreenshots中的“app”名称更改为其他内容,例如“apps”。在这种情况下,必须保留“app”一词。

Try this Query instead. You could do .filter() too on this if you don't want the first entity.

请尝试使用此查询。如果您不想要第一个实体,也可以对此执行.filter()。

class AppScreenshots(db.Model):
     image_file     = db.StringProperty()
     description    = db.StringProperty()
     apps            = db.ReferenceProperty(Apps)

appsObject = db.Query(Apps).get()

ss = AppScreenshots(image_file = 'foo', description = 'bar',apps = appsObject)

Here is a helpful article on modeling relationships link.

这是一篇关于建模关系链接的有用文章。

Also a related question here on SO

这也是一个相关的问题

#1


The problem I found when trying to run your code was that apparently you need to change the name of 'app' in AppScreenshots to something else such as 'apps'. The word 'app' must be reserved in this context.

我在尝试运行代码时遇到的问题是,显然您需要将AppScreenshots中的“app”名称更改为其他内容,例如“apps”。在这种情况下,必须保留“app”一词。

Try this Query instead. You could do .filter() too on this if you don't want the first entity.

请尝试使用此查询。如果您不想要第一个实体,也可以对此执行.filter()。

class AppScreenshots(db.Model):
     image_file     = db.StringProperty()
     description    = db.StringProperty()
     apps            = db.ReferenceProperty(Apps)

appsObject = db.Query(Apps).get()

ss = AppScreenshots(image_file = 'foo', description = 'bar',apps = appsObject)

Here is a helpful article on modeling relationships link.

这是一篇关于建模关系链接的有用文章。

Also a related question here on SO

这也是一个相关的问题