Python 为每个用户获取今日通知的最佳方式
我正在编写示例应用程序来学习烧瓶,因为我有一些模型Python 为每个用户获取今日通知的最佳方式,python,sqlalchemy,flask,django-queryset,Python,Sqlalchemy,Flask,Django Queryset,我正在编写示例应用程序来学习烧瓶,因为我有一些模型 class Followup(Base): __tablename__ = 'followup' id = Column(Integer, primary_key=True) status = Column(String(), nullable=False) next_date = Column(DateTime, default = datetime.utcnow()) student_id = Column(Integ
class Followup(Base):
__tablename__ = 'followup'
id = Column(Integer, primary_key=True)
status = Column(String(), nullable=False)
next_date = Column(DateTime, default = datetime.utcnow())
student_id = Column(Integer, ForeignKey('student.id'), nullable='True')
prospect = relationship("Student", backref="followup")
class Student(Base):
__tablename__ = 'student'
id = Column(Integer, primary_key=True)
name = Column(String(), nullable=False)
email = Column(String(), nullable=False)
address = Column(String(), nullable=False)
status = Column(String(), nullable=False) #saving active, inactive values
我想获得所有学生的今日跟踪通知(如果存在,我想获得具有今日跟踪记录的学生对象,否则没有后续值)
这里我得到了同一个学生的多个记录
我如何查询以获得每个学生今天的记录
提前谢谢你为什么不做好所有的后续工作,过滤掉那些不活跃的学生呢?学生对象作为
followup.prospect
从followup对象中可用
您可以在SQLAlchemy中执行此操作,或者只需获取所有后续信息并过滤掉非活动状态,如下所示:
followups = session.query(Followup).all()
active_followups = [f for f in followups if f.proscpect.status == "active"]
这是在一次往返中获取所有数据的查询:
today = datetime.today()
def get_data():
""" @return: [(student, followup), (student, followup), ...] """
q = (session.query(Student) # get all students
.outerjoin(Followup, and_(Student.id == Followup.student_id, func.date(Followup.next_date)==today)) # @NOTE: check for today might need to be different depending on the database used. Current code should work for mysql
.options(contains_eager(Student.followup)) # let SA know that "followup" is loaded (line above)
.filter(Student.status == "active") # filter only for active students
)
res = [(s, s.followup) for s in q.all()]
return res
data = get_data()
# debug
for s, f in data:
print s.name
for _f in f:
print " ", _f
我想显示模板中的所有用户,如果该用户必须在今天进行后续操作,则为该用户显示一个通知图标(以了解今天的后续操作),而不为其他用户显示任何图标
today = datetime.today()
def get_data():
""" @return: [(student, followup), (student, followup), ...] """
q = (session.query(Student) # get all students
.outerjoin(Followup, and_(Student.id == Followup.student_id, func.date(Followup.next_date)==today)) # @NOTE: check for today might need to be different depending on the database used. Current code should work for mysql
.options(contains_eager(Student.followup)) # let SA know that "followup" is loaded (line above)
.filter(Student.status == "active") # filter only for active students
)
res = [(s, s.followup) for s in q.all()]
return res
data = get_data()
# debug
for s, f in data:
print s.name
for _f in f:
print " ", _f