在创建过程中将作者分配给实体

编程入门 行业动态 更新时间:2024-10-26 04:23:51
本文介绍了在创建过程中将作者分配给实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何分配给一个创建的实体,它自己的 作者。

例如,我有两种ndb.Models种类:

class User(ndb.Model): username = ndb.StringProperty(required = True) bio = ndb.TextProperty(required = True) password = ndb。 StringProperty(required = True) email = ndb.StringProperty() created = ndb.DateTimeProperty(auto_now_add = True) $ b $ class Blog(ndb.Model): title = ndb.StringProperty(required = True) body = ndb.TextProperty(required = True) created = ndb.DateTimeProperty(auto_now_add = True)

当一个 Blog 实体由登录用户创建时,它自己的作者( User 实体)也应该与它一起确定。

最后,我想显示一个博客帖子及其作者信息(例如,作者的 bio )

如何实现这一目标?

解决方案

您的 code> class应该包含一个属性来存储写它的用户的密钥:

author = ndb.KeyProperty (required = True)

然后,您可以在创建Blog实例时设置此属性:

blog = Blog(title =title,body =body,author = user.key)

为了优化,如果您知道登录用户的ndb.Key,并且您不需要用户实体本身,而不需要先获取用户。

assert isinstance(user_key,ndb.Key) blog = Blog(title =title,body =body,author = user_key)

class User(ndb.Model): username = ndb.StringProperty(required = True) password = ndb.StringProperty(required = True) email = ndb.StringProperty() created = ndb.DateTimeProperty(auto_now_add = True) class Blog(ndb.Model): title = ndb.StringProperty(required = True) body = ndb .TextProperty(required = True) created = ndb.DateTimeProperty(auto_now_add = True) author = ndb.KeyProperty(required = True) $ b $ def new_blog(author):为给定作者创建新的博客文章,如果是isinstance(作者,用户),则可能是ndb.Key或User实例: author_key = author.key elif isinstance(author,ndb.Key): assert author.kind()== User._get_kind()#验证提供的ndb.Key是否正确。 author_key = author blog =博客(title =title,body =body,author = author_key)返回博客

如果您将new_blog的开头标准化为效用函数,您可以获得奖励积分。 $ b

I am doing Udacity's Web Dev Course with Google Appengine and Python.

I would like to know how I could assign to a created entity, its own author.

For example, I have two ndb.Models kinds:

class User(ndb.Model): username = ndb.StringProperty(required = True) bio = ndb.TextProperty(required = True) password = ndb.StringProperty(required = True) email = ndb.StringProperty() created = ndb.DateTimeProperty(auto_now_add = True) class Blog(ndb.Model): title = ndb.StringProperty(required = True) body = ndb.TextProperty(required = True) created = ndb.DateTimeProperty(auto_now_add = True)

When a Blog entity is created by a logged-in user, its own author (User entity) should also be identified with it.

Ultimately, I would like to display a blog's post with its author's information (for example, the author's bio)

How can this be achieved?

解决方案

Your Blog class should include a property to store the key of the user who wrote it:

author = ndb.KeyProperty(required = True)

You can then set this property when you create a Blog instance:

blog = Blog(title="title", body="body", author=user.key)

For optimization, if you know the logged in user's ndb.Key, and you don't need the user entity itself, you would pass that directly, instead of needing to fetch the user first.

assert isinstance(user_key, ndb.Key) blog = Blog(title="title", body="body", author=user_key)

In full:

class User(ndb.Model): username = ndb.StringProperty(required = True) password = ndb.StringProperty(required = True) email = ndb.StringProperty() created = ndb.DateTimeProperty(auto_now_add = True) class Blog(ndb.Model): title = ndb.StringProperty(required = True) body = ndb.TextProperty(required = True) created = ndb.DateTimeProperty(auto_now_add = True) author = ndb.KeyProperty(required = True) def new_blog(author): """Creates a new blog post for the given author, which may be a ndb.Key or User instance""" if isinstance(author, User): author_key = author.key elif isinstance(author, ndb.Key): assert author.kind() == User._get_kind() # verifies the provided ndb.Key is the correct kind. author_key = author blog = Blog(title="title", body="body", author=author_key) return blog

You may get bonus points if you standardize the beginning of new_blog to a utility function.

更多推荐

在创建过程中将作者分配给实体

本文发布于:2023-10-29 03:24:41,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   作者   过程中将

发布评论

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

>www.elefans.com

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