I am trying to buit a classmethod, which returns number of members associated with a project. I tried:
# method of class Project
@classmethod
def member_count(cls, project_id):
return Session.query(ProjectMember).
filter(ProjectMember.project_id==project_id).count()
The many-to-many relationship is defined as:
class Member(Base):
__tablename__ = 'member'
id = Column(Integer, primary_key=True)
login = Column(String(50), unique=True, nullable=False)
project_list = relationship("ProjectMember", backref="member")
class Project(Base):
__tablename__ = 'project'
id = Column(Integer, primary_key=True)
name = Column(String(100), unique=True, nullable=False)
class ProjectMember(Base):
__tablename__ = 'project_member'
project_id = Column(Integer, ForeignKey("project.id"), nullable=False, primary_key=True)
member_id = Column(Integer, ForeignKey("member.id"), nullable=False, primary_key=True)
project = relationship("Project", backref = "project_member")
is_pm = Column(Boolean, default = True, nullable = False)
UniqueConstraint('project_id', 'member_id')
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…