Google App Engine Python 数据存储
基本上我试图制作的是一个数据结构,其中包含用户名、ID 和加入日期。然后我想要一个“子结构”,其中包含用户“文本”及其修改日期。并且用户将拥有此文本的多个实例。
class User(db.Model):
ID = db.IntegerProperty()
name = db.StringProperty()
datejoined = db.DateTimeProperty(auto_now_add=True)
class Content(db.Model):
text = db.StringProperty()
datemod= db.DateTimeProperty(auto_now_add = True)
代码设置是否正确?
Basically what Im trying to make is a data structure where it has the users name, id, and datejoined. Then i want a "sub-structure" where it has the users "text" and the date it was modified. and the user will have multiple instances of this text.
class User(db.Model):
ID = db.IntegerProperty()
name = db.StringProperty()
datejoined = db.DateTimeProperty(auto_now_add=True)
class Content(db.Model):
text = db.StringProperty()
datemod= db.DateTimeProperty(auto_now_add = True)
Is the code set up correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将遇到的一个问题是,使
User.ID
具有唯一性并非易事。问题是,对数据库的两次写入可能发生在不同的分片上,两者几乎同时检查是否存在与唯一性约束匹配的现有条目,但没有找到任何条目,然后都创建相同的条目(关于唯一属性),然后您数据库状态无效。为了解决这个问题,appengine 提供了一种方法来确保某些数据存储实体始终放置在同一台物理计算机上。为此,您可以使用实体键来告诉谷歌如何组织实体。假设您希望用户名是唯一的。将
User
更改为如下所示:是的,确实如此。没有用户名,因为它将在密钥中使用,因此不需要单独出现。如果您愿意,您可以这样做...
要创建
User
的实例,您现在需要做一些不同的事情,您需要在中指定一个key_name
初始化方法。好吧,您确实想确保用户不会互相覆盖,因此您需要将用户创建包装在事务中。首先定义一个执行必要检查的函数:
然后,以这种方式调用它
要查找用户,您只需执行以下操作:
接下来,您需要某种方法将内容与其用户关联起来,反之亦然。一种解决方案是通过将用户声明为内容的父级,将内容放入与用户相同的实体组中。为此,您根本不需要更改内容,但创建它的方式略有不同(就像您对 User 所做的那样):
因此,给定一个内容项,您可以通过检查其键来查找用户:
相反,查找特定用户的所有内容只是稍微棘手一点。
One problem you will have is that making
User.ID
unique will be non-trivial. The problem is that two writes to the database could occur on different shards, both check at about the same time for existing entries that match the uniqueness constraint and find none, then both create identical entries (with regard to the unique property) and then you have an invalid database state. To solve this, appengine provides a means of ensuring that certain datastore entities are always placed on the same physical machine.To do this, you make use of the entity keys to tell google how to organize the entities. Lets assume you want the username to be unique. Change
User
to look like this:Yes, that's really it. There's no username since that's going to be used in the key, so it doesn't need to appear separately. If you like, you can do this...
To create an instance of a
User
, you now need to do something a little different, you need to specify akey_name
in the init method.Well, really you want to make sure that users don't overwrite each other, so you need to wrap the user creation in a transaction. First define a function that does the neccesary check:
Then, invoke it in this way
To find a user, you just do this:
Next, you need some way to associate the content to its user, and visa versa. One solution is to put the content into the same entity group as the user by declaring the user as a parent of the content. To do this, you don't need to change the content at all, but you create it a little differently (much like you did with User):
So, given a content item, you can look up the user by examining its key:
Going in reverse, looking up all of the content for a particular user is only a little trickier.
是的,如果您需要更多文档,可以在此处查看 了解数据库类型,此处了解有关模型的详细信息类。
Yes, and if you need more documentation, you can check here for database types and here for more info about your model classes.
您可能会看到的另一种解决方案是使用引用属性。
An alternative solution you may see is using referenceproperty.