推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
lbfeng
V2EX  ›  Python

Flask-sqlalchemy hybrid_property 能被 query 吗?

  •  
  •   lbfeng · Nov 9, 2016 · 2708 views
    This topic created in 3609 days ago, the information mentioned may be changed or developed.

    有 3 个 class Class, Student, Homework 。 Class 对 Student 一对多, Student 对 Homework 一对多。

    在 Class 中有 hybrid_property 返回 Class 中的 Homework 并集.

    @hybrid_property
    def homeworks(self):
    	return set([student.homeworks.all() for student in self.students.all()])
        
    @homeworks.expression
    def homeworks(cls):
        return select([distinct(Homework)]).where(and_(Student.class_id == cls.id, Student.homework_id == Homework.id)).as_scalar()
    

    现在想返回某个 Class 的 deadline 在 2016.11.11 的所有 homework 。

    one_class.homeworks.filter(Homework.deadline == traget_date).all()
    

    产生错误 homeworks Set object 没有 filter 方法。

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2707 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 04:03 · PVG 12:03 · LAX 21:03 · JFK 00:03
    ♥ Do have faith in what you're doing.