[英]Types in Python - Google Appengine
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.
我以为设置bool(self.reque ....)会设置字符串的类型,但是不管我通过什么,它仍然将其存储为TRUE。 I had the same issue with using required=True on strings for the model;
我在模型的字符串上使用required = True时遇到了相同的问题; 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. 您已经添加了许多复杂的层来了解bool()内置函数的功能。 Why don't you test it out directly on the command-line, before embedding it deep in your google app engine code.
为什么不直接将其嵌入到Google App Engine代码中,然后直接在命令行上对其进行测试。
What you'd discover is that the bool() function uses python's truth values: 您会发现bool()函数使用python的true值:
http://docs.python.org/library/stdtypes.html#truth-value-testing 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
. 特别是-任何非空字符串都是
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.
我以为设置bool(self.reque ....)会设置字符串的类型,但是不管我通过什么,它仍然将其存储为TRUE。
Well, what's the value of self.request.get('MAIL')
? 那么,
self.request.get('MAIL')
的价值是什么? If it's anything other than an empty string or None
, bool
will see it as True
. 如果不是空字符串或
None
,则bool
会将其视为True
。
I had the same issue with using required=True on strings for the model;
我在模型的字符串上使用required = True时遇到了相同的问题; 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. 如果将属性设置为
required=True
,则必须将其传递给模型的构造函数。 So, if WebSite
is a required property, you need to construct your searchRec
like so: 因此,如果
WebSite
是必需属性,则需要像下面那样构造searchRec
:
searchRec = SearchRec(WebSite=self.request.get('WEBSITE'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.