要获取当前组织中的成员列表,我使用以下语句
from ..members import members
from flask.ext.login import current_user
from app.common.database import db_session
@members.route("/", methods=["GET"])
@login_required
def index():
datas = db_session.query(Group_has_Person).filter(Group_has_Person.group.organization==current_user.organization).all()
但在运行时抛出异常:AttributeError:与“Group_has_Person.group”相关联的“InstrumentedAttribute”对象和“Comparator”对象都没有“组织”属性
如何以正确的方式获取会员列表?下面的代码是关于模型定义:
class Organization(Base):
__tablename__ = 'Organization'
id = Column(Integer, primary_key = True)
title = Column(String(45), nullable = False)
logo = Column(String(256), nullable = False)
contact_name = Column(String(45), nullable = False)
contact_phone = Column(String(45), nullable = False)
created_time = Column(DateTime, nullable = False, default = datetime.now())
User_id = Column(Integer, ForeignKey('User.id'))
user = relationship("User", back_populates="organization")
groups = relationship("Group", back_populates="organization")
class Group(Base):
__tablename__ = 'Group'
id = Column(Integer, primary_key = True)
title = Column(String(45), nullable = False)
teacher = Column(String(45), nullable = True)
contact = Column(String(45), nullable = True)
created_time = Column(DateTime, nullable = False, default = datetime.now())
status = Column(Integer, nullable = False, default = 1)
Organization_id = Column(Integer, ForeignKey('Organization.id'))
organization = relationship("Organization", back_populates="groups")
members = relationship("Group_has_Person", back_populates="group")
class Person(Base):
__tablename__ = 'Person'
id = Column(Integer, primary_key = True)
nickname = Column(String(45), nullable = False)
avatar = Column(String(256), nullable = False)
gender = Column(Integer, nullable = False)
birthday = Column(DateTime, nullable = False)
User_id = Column(Integer, ForeignKey('User.id'))
user = relationship("User", back_populates="person")
groups = relationship("Group_has_Person", back_populates="person")
class Group_has_Person(Base):
__tablename__ = 'Group_has_Person'
Group_id = Column(Integer, ForeignKey('Group.id'), primary_key = True)
Person_id = Column(Integer, ForeignKey('Person.id'), primary_key = True)
created_time = Column(DateTime, nullable = False, default = datetime.now())
status = Column(Integer, nullable = False, default = 0)
group = relationship("Group", back_populates="members")
person = relationship("Person", back_populates="groups")
表脚本:
CREATE TABLE 'Group' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'title' varchar(45) NOT NULL DEFAULT '' COMMENT '班级名称',
'teacher' varchar(45) DEFAULT NULL COMMENT '辅导老师',
'contact' varchar(45) DEFAULT NULL COMMENT '联系方式',
'created_time' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
'status' int(11) NOT NULL COMMENT '状态:-1-删除 1-正常',
'Organization_id' int(11) NOT NULL,
PRIMARY KEY ('id'),
KEY 'fk_Classes_Organization1_idx' ('Organization_id'),
CONSTRAINT 'fk_Classes_Organization1' FOREIGN KEY ('Organization_id') REFERENCES 'Organization' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='班级';
CREATE TABLE 'Person' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'User_id' int(11) NOT NULL,
'nickname' varchar(45) NOT NULL COMMENT '',
'avatar' varchar(256) NOT NULL COMMENT '',
'gender' int(11) NOT NULL COMMENT '',
'birthday' datetime DEFAULT NULL COMMENT '',
PRIMARY KEY ('id'),
KEY 'fk_Person_User1_idx' ('User_id'),
CONSTRAINT 'fk_Person_User1' FOREIGN KEY ('User_id') REFERENCES 'User' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='个人';
CREATE TABLE 'Group_has_Person' (
'Group_id' int(11) NOT NULL,
'Person_id' int(11) NOT NULL,
'created_time' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
'status' int(11) NOT NULL DEFAULT '0' COMMENT '状态 0-申请 1-成功',
PRIMARY KEY ('Group_id','Person_id'),
KEY 'fk_Group_has_Person_Person1_idx' ('Person_id'),
KEY 'fk_Group_has_Person_Group1_idx' ('Group_id'),
CONSTRAINT 'fk_Group_has_Person_Group1' FOREIGN KEY ('Group_id') REFERENCES 'Group' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT 'fk_Group_has_Person_Person1' FOREIGN KEY ('Person_id') REFERENCES 'Person' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
答案 0 :(得分:0)
我通过以下声明解决了我的问题:
datas = db_session.query(Group_has_Person).join(Group).filter(Group.organization==current_user.organization).all()