PROBLEM WITH .CONTAINS() ON GAE

54 views
Skip to first unread message

PRACHI VAKHARIA

unread,
Jun 22, 2016, 3:51:44 AM6/22/16
to web2py-users


PROBLEM WITH .CONTAINS() ON GAE


The following function does not work on GAE – the line in Red.

MODEL

htmlDB.define_table('Articles',
    Field('Title'),
    Field('HtmlBody', 'text', label="Enter Full Article"),
    Field('Abstract', 'text', label="Enter Abstract"),
    Field('Author'),
    format='%(Title)s'
    )


CONTROLLER FUNCTION

def HomeList():
    if request.args(0):
        AuthorQuery = htmlDB.Articles.Author.contains(request.args(0))
        List = htmlDB(AuthorQuery).select(htmlDB.Articles.ALL, orderby=~htmlDB.Articles.Dated)

    else:
        List = htmlDB().select( htmlDB.Articles.ALL, orderby=~htmlDB.Articles.Dated, limitby=(0, 2) )

    return dict(List=List)




For a URL like this
http://........../HomeList.html/AUTHOR NAME
The controller much check if the 'AUTHOR NAME' is in the htmlDB.Articles


How to make .contains() work on GAE?


Please help and guide. Thank you.
— PRACHI —




.

Anthony

unread,
Jun 22, 2016, 8:25:27 AM6/22/16
to web2py-users
I don't think the GAE datastore supports text search (you can use .contains on a list:-type field, though).

Anthony

Massimo Di Pierro

unread,
Jun 23, 2016, 5:28:53 AM6/23/16
to web2py-users
I can confirm it does not.

PRACHI VAKHARIA

unread,
Jun 23, 2016, 9:04:00 PM6/23/16
to web2py-users


Thank you, Anthony and Massimo!

What if I do not want Lists, but only a simple text field? There will be only one string field which is the name of a person (Author).
How will using list:<type> be different from the simple text field?
And does .contains() work at all on GAE?


I am still a beginner, and hence the questions. I want to understand what happens in the backside/DB-side too.

Thank you, once again!

– PRACHI –

PRACHI VAKHARIA

unread,
Jun 23, 2016, 9:32:12 PM6/23/16
to web2py-users


NEW MODIFIED CODE — STILL DOES NOT FUNCTION

The following function does not work on GAE – the lines in Red.

MODEL

htmlDB.define_table('Articles',
    Field('Title'),
    Field('HtmlBody', 'text', label="Enter Full Article"),
    Field('Abstract', 'text', label="Enter Abstract"),
    Field('Author', 'list:string'),
    format='%(Title)s'
    )

CONTROLLER FUNCTION

def HomeList():
    if request.args(0):
        AuthorQuery = htmlDB.Articles.Author.contains(request.args(0))
        List = htmlDB(AuthorQuery).select(htmlDB.Articles.ALL, orderby=~htmlDB.Articles.Dated)

    else:
        List = htmlDB().select( htmlDB.Articles.ALL, orderby=~htmlDB.Articles.Dated, limitby=(0, 2) )

    return dict(List=List)




For a URL like this
http://........../HomeList.html/AUTHOR NAME
The controller much check if the 'AUTHOR NAME' is in the htmlDB.Articles


What should I do or change to make the above work? Can anyone post the change in code or Field type required?

Thank you, again!


— PRACHI —




Anthony

unread,
Jun 23, 2016, 9:54:59 PM6/23/16
to web2py-users
No, I was not recommending changing the field type to list:string -- that isn't the appropriate field type for your data. I was just mentioning that .contains only works on list fields, and in that case, it is looking for complete word matches within the list. The bottom line is that you cannot do this using the App Engine datastore -- that is one of its limitations. You could instead consider using the Search API, but you'll need a way to get the data into the search indexes, and you won't be able to use the DAL for queries.

Note, if you want to simulate a .startswith query, I think you can do htmlDB.Articles.Author >= request.args(0).

Anthony

PRACHI VAKHARIA

unread,
Jun 23, 2016, 11:53:35 PM6/23/16
to web...@googlegroups.com



Thank you, Anthony. The Google Search API is a little too difficult for me to master, and also is billed.
I tried your other suggestion for simulating the .startswith query, but it did not work in my case, either.


MODIFIED CONTROLLER FUNCTION

def HomeList():
    if request.args(0):
        AuthorQuery = htmlDB.Articles.Author >= request.args(0)

        List = htmlDB(AuthorQuery).select(htmlDB.Articles.ALL, orderby=~htmlDB.Articles.Dated)
    else:
        List = htmlDB().select( htmlDB.Articles.ALL, orderby=~htmlDB.Articles.Dated, limitby=(0, 2) )

    return dict(List=List)



Is there any way to make it work like this on GAE:
AuthorQuery = htmlDB.Articles.Author == request.args(0)


Thank you, very much, once again, even though we are still far from a solution.

— PV —


Anthony

unread,
Jun 24, 2016, 8:49:47 AM6/24/16
to web2py-users
On Thursday, June 23, 2016 at 11:53:35 PM UTC-4, PRACHI VAKHARIA wrote:



Thank you, Anthony. The Google Search API is a little too difficult for me to master, and also is billed.
I tried your other suggestion for simulating the .startswith query, but it did not work in my case, either.

What do you mean by "did not work"? Did you get an error, or it just didn't return the results you expected? Note, searches are also case sensitive, so your query would have to match the exact beginning of a field's value, including the case.

If you use ._select instead of .select, instead of running the query, web2py will give you the GQL code that it generates for the query. Maybe inspect that to see if it as expected. You could play around with it and submit it via db.executesql() to see if you can come up with a working query.

Anthony

Anthony

unread,
Jun 24, 2016, 8:51:41 AM6/24/16
to web2py-users
You might also consider Google Cloud SQL, which the DAL also supports.

Anthony
Reply all
Reply to author
Forward
0 new messages