GQL with two tables
Hello i am doing a very small application in google appengine and 开发者_JAVA百科i use python. My problem is that i have two tables using de db.model ("clients" and "requests"). The table "client" has got the email and name fields and the table "requests" has got the email and issue fields. I want to do a query that returns for each request the email, issue and client name, if the email is the same in the two tables. Can anyone help, please?
The app engine datastore does not support joins, so you will not be able to solve this problem with GQL. You can use two gets, one for client and one for request, or you can use a ReferenceProperty to establish a relationship between the two entities.
If you need to model a one-to-many relationship, you can do it with a reference property. For your case, it would look something like this:
class Client(db.Model):
email = db.UserProperty()
name = db.StringProperty()
class Request(db.Model):
client = db.ReferencePrpoerty(Client, collection_name='requests')
issue = db.StringProperty()
Any Client entity that has a Request associated with it will automatically get a property called requests which is a Query object that will return all Request entities that have a client field set to the particular Client entity you are dealing with.
You might also want to make sure that the code that creates Request entities set each new entity to have the Client entity for the particular user as its ancestor. Keeping these associated items in the same entity group could be helpful for performance reasons and transactions.
using this models:
class Client(db.Model):
email = db.StringProperty()
name = db.StringProperty()
class Request(db.Model):
client = db.ReferenceProperty(Client, collection_name='requests')
issue = db.StringProperty()
With this code can query the data
from modelos import Client,Request
ctes=Client.all().filter("email =","somemail@mailbox.com.mx")
for ct in ctes:
allRequest4ThisUser=Request.all().filter("client =",ct)
for req in allRequest4ThisUser:
print req.issue
精彩评论