如何设置 ndb keyProperty [英] How to set an ndb keyProperty

查看:18
本文介绍了如何设置 ndb keyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解实体和键在 Google App Engine NDB 中的工作方式时遇到了一些问题.

I'm having some trouble understanding how entities and keys work in Google App Engine NDB.

我有一个 post 实体和一个 user 实体.如何将 post 上的 user_key 设置为 user?

I have a post entity and a user entity. How do I set the user_key on post to user?

在交互式控制台中,到目前为止我有这个:

In the interactive console, I have this so far:

from google.appengine.ext import ndb
from app.lib.posts import Post
from app.lib.users import User
from random import shuffle

users = User.query()
posts = Post.query().fetch()

for post in posts:
  post.user_key = shuffle(users)[0]
  post.put()

我只是想设置一些用于开发的种子数据.我知道这可能不是设置事物的理想方式,但我的第一个问题是:

I'm just trying to set up some seed data for development. I know this probably isn't the ideal way to set things, but my first question is:

  1. 如何从实体获取密钥(文档中描述了相反的内容)
  2. 如何在 ndb 中设置关联?

推荐答案

尝试:

post.user_key = shuffle(users)[0].key

这篇关于如何设置 ndb keyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆