gpt4 book ai didi

python - 使用谷歌应用引擎(Python)查询多个表

转载 作者:行者123 更新时间:2023-11-30 23:54:28 24 4
gpt4 key购买 nike

我有三个表,1-用户、2-软件、3-用户软件。

如果假设,用户表有 6 个用户记录(比如 U1、U2、...、U6),软件表有 4 个不同的软件(比如 S1、S2、S3、S4),如果用户请求,则 UserSoftwares 存储引用仅适用于给定软件。例如:UserSoftwares(5 条记录)只有两列(userid、softwareid),它们引用了其他列。数据是:

U1 S1

U2 S2

U2 S3

U3 S3

U4 S1

现在我期待以下结果:(如果当前登录用户是 U2):

<小时/>

S1 禁用

S2 启用

S3 启用

S4 禁用

这里,第一列是软件ID或名称,第二列是状态,它只有两个值(启用/禁用),基于UserSoftwares表(模型)。 注意 status不是任何模型(表)的字段。“我的逻辑是:1. 循环遍历软件模型中的各个软件2. 在 UserSoftwares 模型中查找当前登录用户 ID(U2)的 softwareid: 如果找到则设置 status='Enable' 如果没有找到则设置 status='Disable'3. 将此状态属性添加到软件对象。4. 对所有软件重复此过程。”为了达到上述结果,Python 谷歌应用引擎中应该执行什么查询?

最佳答案

由于 GAE 的数据存储不是关系型,因此您必须在不使用联接的情况下对多对多关系进行建模。您可以轻松地适应以下两种方法来满足您的需求。

使用链接模型方法的工作示例(更新#1)

from google.appengine.ext import db

# Defining models

class User(db.Model):
name = db.StringProperty()


class Software(db.Model):
name = db.StringProperty()
description = db.TextProperty()


class UserSoftwares(db.Model):
user = db.ReferenceProperty(User, collection_name='users')
software = db.ReferenceProperty(Software, collection_name='softwares')

# Creating users

u1 = User(name='John Doe')
u2 = User(name='Jane Doe')

# Creating softwares
sw1 = Software(name='Office 2007')
sw2 = Software(name='Google Chrome')
sw3 = Software(name='Notepad ++')

# Batch saving entities
db.put([u1, u2, sw1, sw2, sw3])

"""
Creating relationship between users and softwares;
in this example John Doe's softwares are 'Office 2007' and
'Notepad++' while Jane Doe only uses 'Google Chrome'.
"""
u1_sw1 = UserSoftwares(user=u1, software=sw1)
u1_sw3 = UserSoftwares(user=u1, software=sw3)
u2_sw2 = UserSoftwares(user=u2, software=sw2)

# Batch saving relationships
db.put([u1_sw1, u1_sw3, u2_sw2])

"""
Selects all softwares.
"""

rs1 = Software.all()

# Print results
print ("SELECT * FROM Software")
for sw in rs1:
print sw.name

"""
Selects a software given it's name.
"""

rs2 = Software.all().filter("name =", "Notepad ++")

# Print result
print("""SELECT * FROM Software WHERE name = ?""")
print rs2.get().name

"""
Selects all software used by 'John Smith'.
"""

# Get John Doe's key only, no need to fetch the entire entity
user_key = db.Query(User, keys_only=True).filter("name =", "John Doe").get()

# Get John Doe's software list
rs3 = UserSoftwares.all().filter('user', user_key)

# Print results
print ("John Doe's software:")
for item in rs3:
print item.software.name

"""
Selects all users using the software 'Office 2007'
"""

# Get Google Chrome's key
sw_key = db.Query(Software, keys_only=True).filter("name =", "Google Chrome").get()

# Get Google Chrome's user list
rs4 = UserSoftwares.all().filter('software', sw_key)

# Print results
print ("Google Chrome is currently used by:")
for item in rs4:
print item.user.name

链接模型方法(推荐)

您可以通过以下方式表示每个表来建模多对多关系:

from google.appengine.ext import db    

class User(db.Model):
name = db.StringProperty()


class Software(db.Model):
name = db.StringProperty()
description = db.TextProperty()


class UserSoftwares(db.Model):
user = db.ReferenceProperty(User, collection_name='users')
software = db.ReferenceProperty(Software, collection_name='softwares')

正如你所看到的,它与关系的思维方式非常相似。

key 列表方法(替代)

关系也可以建模为键列表:

class User(db.Model):
name = db.StringProperty()
softwares = db.ListProperty(db.Key)


class Software(db.Model):
name = db.StringProperty()
description = db.TextProperty()

@property
def users(self):
return User.all().filter('softwares', self.key())

此方法更适合少量键,因为它使用 ListProperty,但比上面的链接模型方法更快

关于python - 使用谷歌应用引擎(Python)查询多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5142192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com