在设置了验证器的属性上查询过滤

时间:2013-04-03 06:41:48

标签: python google-app-engine app-engine-ndb

我的模型User和属性username带有验证器,可以验证username中DB中的可用字符,非空和存在。因此,在处理注册表单时,我不需要任何其他检查 - 例如,我只需将表单username值分配给模型的属性,并在输入{{1}时捕获验证错误已经存在......

但是,它不起作用。

因为NDB也验证了属性的比较参数(请参阅ndb / model.py中的Property._comparison方法),并且它在Query.filter(User.username == [somevalue])中进行无休止的请求并最终引发username。 NDB尝试使用RuntimeError: maximum recursion depth exceeded验证[somevalue]并一次又一次地转到此查询...

可以将用户名分配给实体的ID并使用User.get_by_id(),但需要validate_username才能更改,所以我需要使用{{ 1}}。

所以这是我的username模型:

Query.get()

例如:

User

我做错了什么?什么是解决此类问题的最佳方法?

更新至Tim Hoffman 谢谢。是的,我错过了道具辩论,但方法在class User(ndb.Model): def validate_username(self, value): value = str(value).strip() # Other useful checks - length, available symbols, etc if User.get_user(value): raise ValueError('Username already exists') return value @classmethod def get_user(cls, username): username = str(username) user_q = User.query() user_q = user_q.filter(User.username == username) # Here is the problem return user_q.get() username = ndb.StringProperty(validator=validate_username) # Trying to add user, get RuntimeError exception u = User() u.username = 'John' prop论证中收到了self - 因此我没有提到这个错误。但是,您已经错过了关键问题 - 您不会在验证程序中使用带有过滤器的查询(User.get_user方法)。试试这个,没有任何意义函数或方法验证器是:

val

1 个答案:

答案 0 :(得分:2)

我认为您的问题是由于错误地将验证器定义为方法而未接受正确的参数。请参阅下面的快速示例,适用于过滤器。

The db, ndb, users, urlfetch, and memcache modules are imported.
dev~cash-drawer> def vla(prop,val):
...    if val == "X":
...      raise ValueError
...    return val
... 
dev~cash-drawer> 
dev~cash-drawer> 
dev~cash-drawer> class X(ndb.Model):
...    name = ndb.StringProperty(validator=vla)
... 
dev~cash-drawer> y = X()
dev~cash-drawer> y.name = "X"
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/timh/google_appengine/google/appengine/ext/ndb/model.py", line 1258, in __set__
    self._set_value(entity, value)
  File "/home/timh/google_appengine/google/appengine/ext/ndb/model.py", line 1004, in _set_value
    value = self._do_validate(value)
  File "/home/timh/google_appengine/google/appengine/ext/ndb/model.py", line 953, in _do_validate
    newvalue = self._validator(self, value)
  File "<console>", line 3, in vla
ValueError
dev~cash-drawer> y.name = "aaa"
dev~cash-drawer> y.put()
Key('X', 5060638606280884224)
dev~cash-drawer> z=X.query().filter(X.name == "aaa")
dev~cash-drawer> list(z)
[X(key=Key('X', 5060638606280884224), name=u'aaa')]

    dev~cash-drawer> z=X.query().filter(X.name == "X")
    Traceback (most recent call last):
        File "<console>", line 1, in <module>
        File "/home/timh/google_appengine/google/appengine/ext/ndb/model.py", line 859, in __eq__
        return self._comparison('=', value)
        File "/home/timh/google_appengine/google/appengine/ext/ndb/model.py", line 847, in _comparison
        value = self._do_validate(value)
         File "/home/timh/google_appengine/google/appengine/ext/ndb/model.py", line 953, in _do_validate
        newvalue = self._validator(self, value)
         File "<console>", line 3, in vla
    ValueError
    dev~cash-drawer>