我正在使用 redis 对象映射器,当我尝试运行这段代码时:
from redis import *
from rom import util
from rom import *
util.set_connection_settings(host='localhost', db=7)
class User(Model):
uName=String(required=True)
password=String(required=True)
thisId=(random.random()*100)
user=User(uName='pfleet',password='pass')
user.save()
#todo, save session ID
user2=User.get_by(uName='pfleet')
出于某种原因,当我运行这段代码时,我收到一个错误,提示“未定义名称字符串”,即使我以与 ROM(redis 对象映射器)github 页面上的在线示例相同的方式键入代码.
最佳答案
感谢@univerio 的评论,String
似乎仅在 python2.x 上由 from rom import *
导入,引自 docstring :
note:: this column type is only available in Python 2.x
对于 python3.x,您需要改用 Text
。
class User(Model):
uName=Text(required=True)
password=Text(required=True)
作为旁注,尽量不要使用 from xxx import *
通配符导入 - 它真的是 a bad practice :
Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).
关于python - 为什么我会收到有关 String not defined 的错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251789/