如何设置 ndb keyProperty

编程入门 行业动态 更新时间:2024-10-17 00:30:30
本文介绍了如何设置 ndb keyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在理解实体和键在 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:

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

推荐答案

尝试:

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

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

更多推荐

[db:关键词]

本文发布于:2023-04-17 19:31:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/921499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何设置   ndb   keyProperty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!