我正在使用SQlAlchemy推荐的uuid()支持加载项,如here所述。但是,当我在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
答案 0 :(得分:3)
如果您的文件是GUID.py,并且您将其导入为from app.custom_db import GUID
(就像您拥有的那样),那么您真正导入的是文件,而不是类。要上课,您需要致电GUID.GUID()
。
或者,您可以通过将其导入为:
来导入该类from app.custom_db.GUID import GUID