Getting a bit peeved now;
I have a model and a class thats just storing a get request in the database; basic tracking.
class SearchRec(db.Model):
WebSite = db.StringProperty()#required=True
WebPage = db.StringProperty()
CountryNM = db.StringProperty()
PrefMailing = db.BooleanProperty()
DateStamp = db.DateTimeProperty(auto_now_add=True)
IP = db.StringProperty()
class AddSearch(webapp.RequestHandler):
def get(self):
searchRec = SearchRec()
searchRec.WebSite = self.request.get('WEBSITE')
searchRec.WebPage = self.request.get('WEBPAGE')
searchRec.CountryNM = self.request.get('COUNTRY')
searchRec.PrefMailing = bool(self.request.get('MAIL'))
searchRec.IP = self.request.get('IP')
Bool has my biscuit; I thought that setting bool(self.reque....) would set the type of the string but no matter what I pass it it still stores it as TRUE in the database. I had the same issue with using required=True on strings for the model; the damn thing kept saying that nothing was being passed... but it had.
Ta
You've added a lot of layers of complication to understanding what the bool() build-in function does. Why don't you test it out directly on the command-line, before embedding it deep in your google app engine code.
What you'd discover is that the bool() function uses python's truth values:
http://docs.python.org/library/stdtypes.html#truth-value-testing
The following values are considered false:
* None * False * zero of any numeric type, for example, 0, 0L, 0.0, 0j. * any empty sequence, for example, '', (), []. * any empty mapping, for example, {}. * instances of user-defined classes, if the class defines a `__nonzero__()` or `__len__()` method, when that method returns the integer zero or bool value False. [1]
All other values are considered true — so objects of many types are always true.
In particular - any non-empty string is True
.
Bool has my biscuit; I thought that setting bool(self.reque....) would set the type of the string but no matter what I pass it it still stores it as TRUE in the database.
Well, what's the value of self.request.get('MAIL')
? If it's anything other than an empty string or None
, bool
will see it as True
.
I had the same issue with using required=True on strings for the model; the damn thing kept saying that nothing was being passed... but it had.
If you set a property to required=True
, then you must pass it to the model's constructor. So, if WebSite
is a required property, you need to construct your searchRec
like so:
searchRec = SearchRec(WebSite=self.request.get('WEBSITE'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.