问题描述
我想知道如何分配给一个创建的实体,它自己的 作者。
例如,我有两种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.
这篇关于在创建过程中将作者分配给实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!