使用Google App Engine (Python) 查询多个表

1 投票
3 回答
877 浏览
提问于 2025-04-16 12:40

我有三个表,分别是:1-用户表(Users),2-软件表(Softwares),3-用户软件表(UserSoftwares)。

假设用户表里有6条用户记录(比如U1、U2、...、U6),软件表里有4个不同的软件(比如S1、S2、S3、S4),而用户软件表则存储用户请求的软件的记录。

例如:用户软件表(有5条记录)只有两列,分别是用户ID(userid)和软件ID(softwareid),这些ID是用来关联其他表的。数据如下:

U1 S1

U2 S2

U2 S3

U3 S3

U4 S1

现在我希望得到以下结果:(假设当前登录的用户是U2):


S1 禁用

S2 启用

S3 启用

S4 禁用

这里,第一列是软件ID或名称,第二列是状态(status),状态只有两个值(启用/禁用),这个状态是根据用户软件表来决定的。注意,状态并不是任何表的字段。

我的逻辑是: 1. 遍历软件表中的每个软件 2. 在用户软件表中查找当前登录用户ID(U2)对应的软件ID: 如果找到了,就把状态设为'启用' 如果没找到,就把状态设为'禁用' 3. 将这个状态属性添加到软件对象中。 4. 对所有软件重复这个过程。

那么在Python的Google App Engine中,应该如何写查询来实现以上结果呢?

3 个回答

0

根据这篇关于建模实体关系的数据存储文章,你可以把这个关系建模得像传统的多对多关系,和关系型数据库(RDBMS)类似。

from google.appengine.ext import db
class User(db.Model):
    name = db.StringProperty()

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

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

# use the models like so:

alice = User(name='alice')
alice.put()

s1 = Software(name='s1')
s1.put()

us = UserSoftware(user=alice,software=s1)
us.put()

希望这对你有帮助。

0

如果你在找 join 的话,GAE(谷歌应用引擎)是没有这个功能的。顺便说一下,做两个简单的查询(SoftwaresUserSoftware)其实很简单,然后你可以手动计算所有额外的数据。

3

因为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')

如你所见,这种方式和关系型数据库的思维方式非常相似。

键列表方法(替代方案)

关系也可以通过键的列表来建模:

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,但比上面的链接模型方法

撰写回答