Using the query interface or building a query in code (with filter) are the
same. Neither is capable of the kind of join you want to do.
Two "simple" ways to accomplish what you are trying to do are:
Method 1:
Using your current model, structure the question as:
theowner = PetOwner.all().filter('name =', 'johndoe').get()
print theowner.name
for pet in theowner.pet_set:
print pet.name, pet.type
This will use a bunch of API calls though, as I recall.
Method 2:
Add a "ownerName" (db.StringProperty()) to your Pet model, when you set the
owner also set the ownerName. Then your query could look like:
johnspets = Pet.all().filter('ownerName =', 'johndoe').fetch(limit=1000)
for pet in johnspets:
print pet.ownerName, pet.name, pet.type
This is the method I have been using, a lot. It make reporting much easier
in the reporting intensive apps I develop.
Robert
On Tue, Sep 15, 2009 at 5:17 PM, PatHaugen <[email protected]> wrote:
>
> Thanks for the response, works nice, however coming from PHP/MySQL
> this seems strange to me.
>
> We have two 'tables' and are trying to perform a query to link them,
> however this solution causes two queries on the 'database' to be
> performed, which are logged against you for your quotas.
>
> I'm used to joins:
> http://dev.mysql.com/doc/refman/5.0/en/join.html
>
> One query linking two or more tables for the data you need.
>
> Is Google App Engine's 'query' function not able to do anything like
> this? Is GQL better than GAE's 'Query'?
>
> On Sep 15, 12:41 pm, Arun Shanker Prasad <[email protected]>
> wrote:
> > Hi,
> >
> > I am sorry I replied to the thread when I was not at my dev machine,
> > did not test the code. I am not sure if putting the query inside the
> > query.filter() will break it.
> >
> > owner = PetOwner.all().filter('name =', 'johndoe'),get()
> > query.filter('owner = ', owner)
> >
> > This should work.
> >
> > Thanks,
> > Arun Shanker Prasad.
> >
> > On Sep 16, 12:20 am, PatHaugen <[email protected]> wrote:
> >
> > > I tested the filter you provided:
> > > query.filter('owner = ', PetOwner.all().filter('name =', 'johndoe'))
> >
> > > However it didn't work.
> >
> > > I visited the doc you referenced, but the page does not mention
> > > anything about the style of query filters or any information on
> > > construction of queries with ReferenceProperty that I could find.
> >
> > > Does the query filter you wrote work on your side?
> >
> > > I broke our your query:
> > > PetOwner.all().filter('name =', 'johndoe')
> >
> > > Which worked fine, however it was in placing it inside the
> > > query.filter that I get an error.
> >
> > > Do you not get an error structured like this?
> >
> > > On Sep 15, 4:36 am, Arun Shanker Prasad <[email protected]>
> > > wrote:
> >
> > > > Hi,
> >
> > > > I think you are trying to filter the ReferenceProperty as a string,
> > > > this won't work. You need to;
> >
> > > > query.filter('owner = ', PetOwner.all().filter('name =', 'johndoe'))
> >
> > > > Doc on the Datastore ReferenceProperty;
> >
> > > >
> http://code.google.com/appengine/docs/python/datastore/entitiesandmod...
> >
> > > > Thanks,
> > > > Arun Shanker Prasad.
> >
> > > > On Sep 15, 8:54 am, PatHaugen <[email protected]> wrote:
> >
> > > > > Jumping into GAE, I found the ReferenceProperty interesting but
> still
> > > > > don't fully understand it.
> >
> > > > > My question can expand on the example from:
> http://code.google.com/appengine/docs/python/datastore/creatinggettin...
> >
> > > > > class PetOwner(db.Model):
> > > > > name = db.StringProperty()
> > > > > class Pet(db.Model):
> > > > > name = db.StringProperty()
> > > > > type = db.StringProperty() # cat, dog, etc.
> > > > > owner = db.ReferenceProperty(PetOwner)
> >
> > > > > Let's do a query to select all cats with a specific owner.
> >
> > > > > query = db.Query(Pet)
> > > > > query = Pet.all()
> > > > > query.filter('type = ', 'Cat')
> > > > > results = query.fetch(limit=10)
> > > > > for result in results:
> > > > > output = result.name
> > > > > return output
> >
> > > > > What can I add to this to filter only those where 'owner' is
> > > > > 'johndoe'?
> >
> > > > > I tried:
> > > > > query.filter('owner = ', 'johndoe')
> >
> > > > > However it fails to return me a result, and I know you can pull the
> > > > > owner name via 'pet.owner.name' however trying:
> > > > > query.filter('owner.name = ', 'johndoe')
> >
> > > > > Also fails, though it made sense for what should work. Does anyone
> > > > > know the proper method?
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---