我有一个User Class和一个具有多对多关系的Event类,表示用户是"成员"这件事。当新用户注册某个事件时,我需要检查他们是否已经是会员。
class User(db.Model,UserMixin):
__tablename__ = 'user'
id = db.Column(db.Integer(),primary_key =True)
firstname = db.Column(db.String(50))
lastname = db.Column(db.String(50))
password = db.Column(db.String(255))
username = db.Column(db.String(50), index = True)
email = db.Column(db.String(255), index = True)
create_date = db.Column(db.DateTime())
questions = db.relationship('Question',backref='user',lazy='dynamic')
events = db.relationship('Event',backref='owner',lazy='dynamic')
memberships = db.relationship('Event',secondary=memberships,backref=
db.backref('members',lazy='dynamic'))
class Event(db.Model):
__tablename__ = 'event'
id = db.Column(db.Integer(),primary_key = True)
name = db.Column(db.String(255))
event_code = db.Column(db.String(30))
description = db.Column(db.Text())
owner_id = db.Column(db.Integer(),db.ForeignKey('user.id'))
#owner = db.relationship("User",back_populates="events")
questions = db.relationship('Question',backref='event',lazy='dynamic')
moderate_questions = db.Column(db.Boolean,nullable=False,default=True)
allow_anonymous = db.Column(db.Boolean,nullable=False,default=True)
然后我试图测试一个用户是否已经成为"成员"由关系创建的集合。以下功能不起作用。
def is_member(self, member_id):
return Event.query.filter(Event.members.any
('user_id' == member_id)).filter(Event.members.any('event_id'== self.id)).all()
我的问题是。如何测试值关系是否存在。在多对多的关系中。