Python 2.7 验证属性失败google数据存储

Python 2.7 验证属性失败google数据存储,python-2.7,google-app-engine,google-cloud-datastore,Python 2.7,Google App Engine,Google Cloud Datastore,我有以下基于ndb.model的模型。我试图验证模型,出生日期属性 from google.appengine.ext import ndb class CFCSocialUser(ndb.Model): def clean_dob(value): if value.year < 1900: raise Exception("Year cannot be less than 1900") username = ndb.Stri

我有以下基于ndb.model的模型。我试图验证模型,出生日期属性

from google.appengine.ext import ndb


class CFCSocialUser(ndb.Model):

    def clean_dob(value):
        if value.year < 1900:
            raise Exception("Year cannot be less than 1900")

    username = ndb.StringProperty()
    userid = ndb.IntegerProperty()
    email = ndb.StringProperty()
    date_of_birth = ndb.DateProperty(validator=clean_dob)

    @staticmethod
    def create_new_user(name, email, date_of_birth):
        app = CFCSocialUser(username=name,
                            email=email,
                            date_of_birth=date_of_birth)
        app.put()
当我尝试运行单元测试时,出现以下错误

======================================================================
ERROR: test_validation (tests.test_CFCSocialUser.TestCFCSocialUser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/vinay/App-Engine/Rainbow/cfc-social-media-website/tests/test_CFCSocialUser.py", line 26, in test_validation
    self.assertRaises(Exception, create_user_before_1900())
  File "/Users/vinay/App-Engine/Rainbow/cfc-social-media-website/tests/test_CFCSocialUser.py", line 12, in create_user_before_1900
    date_of_birth=date(1899, 3, 11))
  File "/Users/vinay/App-Engine/Rainbow/cfc-social-media-website/mainsite/rainbow/models/CFCSocialUser.py", line 19, in create_new_user
    date_of_birth=date_of_birth)
  File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 2947, in __init__
    self._set_attributes(kwds)
  File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 2993, in _set_attributes
    prop._set_value(self, value)
  File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1145, in _set_value
    value = self._do_validate(value)
  File "/Users/vinay/google-cloud-sdk/platform/google_appengine/google/appengine/ext/ndb/model.py", line 1094, in _do_validate
    newvalue = self._validator(self, value)
TypeError: clean_dob() takes exactly 1 argument (2 given)
clean_dob()
是一个实例成员函数,应该获得一个
self
作为第一个参数

比如:

也可以将其定义为静态方法:

class MyModel(ndb.Model):
  @staticmethod
  def clean_dob(value):
    pass
或者离开教室:

def clean_dob(value):
  pass

class MyModel(ndb.Model):
  # ...
clean_dob()
是一个实例成员函数,应该获得一个
self
作为第一个参数

比如:

也可以将其定义为静态方法:

class MyModel(ndb.Model):
  @staticmethod
  def clean_dob(value):
    pass
或者离开教室:

def clean_dob(value):
  pass

class MyModel(ndb.Model):
  # ...
应用程序引擎说,使用两个参数调用验证器函数:
prop
value
,因此您需要将函数更改为:

def clean_dob(prop, value):
    if value.year < 1900:
        raise Exception("Year cannot be less than 1900")
def clean_dob(属性,值):
如果value.year<1900:
引发异常(“年份不能小于1900”)
prop参数是正在验证的属性的实例,而不是模型类,因此它应该作为独立函数放置在类主体之外。将其留在类中会让代码读者感到困惑,他们可能会认为它是模型的实例方法。

应用程序引擎说,使用两个参数调用验证器函数:
prop
value
,因此您需要将函数更改为:

def clean_dob(prop, value):
    if value.year < 1900:
        raise Exception("Year cannot be less than 1900")
def clean_dob(属性,值):
如果value.year<1900:
引发异常(“年份不能小于1900”)
prop参数是正在验证的属性的实例,而不是模型类,因此它应该作为独立函数放置在类主体之外。将它留在类中会让代码读者感到困惑,他们可能会认为它是模型的实例方法