views:

43

answers:

2

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

+3  A: 

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.

Douglas Leeder
+1  A: 

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'))
Will McCutchen
Mail was set to False. Thanks for answering the second bit :o)
Chris M
Was it set to False, or 'False'? One is a boolean, the other is a string - and self.request.get only returns strings (or None).
Nick Johnson
It was set to False via self.request.get so I expect it was string; I falsely assumed that sticking bool() around it was the equivalent of changing the type. I have to say I preffer my languages with curly brackets and line ending semi-colons. This feels like a more anal version of VB... but its fast which is the big draw (Appengine java is awful)
Chris M