我使用的是SQlAlchemy推荐的uuid()支持插件,如描述的这里。但是,当我在我的SQLAlchemy代码中使用它时,我会收到以下错误:
TypeError: 'module' object is not callable关于模块,GUID。
下面是GUID代码,直接取自源代码:
GUID.py
from sqlalchemy.types import TypeDecorator, CHAR
from sqlalchemy.dialects.postgresql import UUID
import uuid
class GUID(TypeDecorator):
"""Platform-independent GUID type.
Uses Postgresql's UUID type, otherwise uses
CHAR(32), storing as stringified hex values.
"""
impl = CHAR
def load_dialect_impl(self, dialect):
if dialect.name == 'postgresql':
return dialect.type_descriptor(UUID())
else:
return dialect.type_descriptor(CHAR(32))
def process_bind_param(self, value, dialect):
if value is None:
return value
elif dialect.name == 'postgresql':
return str(value)
else:
if not isinstance(value, uuid.UUID):
return "%.32x" % uuid.UUID(value)
else:
# hexstring
return "%.32x" % value
def process_result_value(self, value, dialect):
if value is None:
return value
else:
return uuid.UUID(value)这就是我的模型
user.py
from app import db
from datetime import datetime
from app.custom_db import GUID
class User(db.Model):
__tablename__ = 'users'
id = db.Column(GUID(), primary_key=True)
email = db.Column(db.String(80), unique=True)
name = db.Column(db.String(80))
password = db.Column(db.String(80))
datejoined = db.Column(db.DateTime,default = db.func.now())
def __init__(self, name, email, password):
self.name = name
self.email = email
self.password = password
def __repr__(self):
return '<User %r>' % self.name知道为什么我不能创建这个uuid() PKey吗?
这是完整的回溯
Traceback (most recent call last):
File "./run.py", line 3, in <module>
from app import app
File "/home/achumbley/Pile/app/__init__.py", line 23, in <module>
from models import user
File "/home/achumbley/Pile/app/models/user.py", line 5, in <module>
class User(db.Model):
File "/home/achumbley/Pile/app/models/user.py", line 7, in User
id = db.Column(GUID(), primary_key=True)
TypeError: 'module' object is not callable 发布于 2014-02-09 22:29:21
如果您的文件是GUID.py,并且您像导入from app.custom_db import GUID一样导入它(就像导入它一样),那么真正导入的是文件,而不是类。要进入这个类,您需要调用GUID.GUID()。
或者,您可以通过导入类来导入类,如:
from app.custom_db.GUID import GUIDhttps://stackoverflow.com/questions/21664899
复制相似问题