开发者

Querying on multiple tables using google apps engine (Python)

开发者 https://www.devze.com 2023-02-14 01:57 出处:网络
I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares. if suppose, Users table having 6 user records(say U1,U2,...,U6) and Softwares table having 4 different softwares(say S1,S2,S3,S4) and UserS

I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares.

if suppose, Users table having 6 user records(say U1,U2,...,U6) and Softwares table having 4 different softwares(say S1,S2,S3,S4) and UserSoftwares stores the references if a user requested for given software only. For example: UserSoftwares(5 records) have only two columns(userid, softwareid) which references others. and the data is:

U1 S1

U2 S2

U2 S3

U3 S3

U4 S1

Now I m expe开发者_如何转开发cting following results:(if current login user is U2):


S1 Disable

S2 Enable

S3 Enable

S4 Disable

Here, 1st column is softwareid or name and 2nd column is status which having only two values(Enable/Disable) based on UserSoftwares table(model). Note status is not a field of any model(table). "My Logic is: 1. loop through each software in softwares model 2. find softwareid with current login userid (U2) in UserSoftwares model: if it found then set status='Enable' if not found then set status='Disable' 3. add this status property to software object. 4. repeat this procedure for all softwares. " What should be the query in python google app engine to achieve above result?


since GAE's datastore is not relational you have to model your many-to-many relationship without using joins. Here are two methods you can adapt easily to your needs.

Working example using link model method (UPDATE #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

Link model method (recommended)

You can model a many-to-many relationship by representing each table in this way:

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

As you can see it is quite similiar to the relational's way of thinking.

Key list method (alternative)

Relationships can also be modelled as list of keys:

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())

This approach is more suited for a small number of keys since it uses a ListProperty but is faster than than the link model method above.


If your are looking for join - there is no joins in GAE. BTW, there is pretty easy to make 2 simple queries (Softwares and UserSoftware), and calculate all additional data manually


According to the Modeling Entity Relationships Datastore Article, you can model this somewhat like a tradition Many-to-Many relationship in a 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()

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号