what is the best filter to query a full name datastore property using only the first name?

拜拜、爱过 提交于 2019-12-31 04:51:07

问题


I have this datastore model:

class Person(db.Model):
    person_name = db.StringProperty(required = True)        
    nacionality = db.StringProperty(required = True)
    marital_status = db.StringProperty(required = True)
    profession = db.StringProperty(required = True)
    SSN = db.IntegerProperty(required = True)
    driver_license = db.IntegerProperty(required = True)
    address = db.PostalAddressProperty(required = True)

In this Model, person_name could be something like this: 'Carl Sagan' (there is only on property for the entiry name). But when I query it, this way:

    searched_name = 'Carl'
    p = Person.all()
    persons = p.filter('person_name >=', searched_name)

I got, as a result, names which doesn't begin with 'Carl' or neither have 'Carl' in any part of the name. If I query this way: persons = p.filter('person_name >=', searched_name) I got none result (even 'Carl Sagan' isn't found). So, I'd like to know: what is the best filter to this kind of query? (quering a complete name property using only the first name)?


回答1:


String filter works as it was comparing chars starting beginning of string. You can do something like this:

p.filter('person_name >=', searched_name)
p.filter('person_name <', searched_name + u'\ufffd')


来源:https://stackoverflow.com/questions/11902019/what-is-the-best-filter-to-query-a-full-name-datastore-property-using-only-the-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!