推荐学习书目
› 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
loyalist
V2EX  ›  Python

django orm 求指教子查询 Subquery 中如何拿到 外部查询 某个值

  •  
  •   loyalist · May 18, 2020 · 2109 views
    This topic created in 2322 days ago, the information mentioned may be changed or developed.
    Ad.objects.annotate(count=Count('*') / Subquery(Img.objects.filter(ad_id='#####').values())
    

    如上述代码中 ###部分我想拿到 外部表 Ad 的 id 值,在子查询中使用 F 查询,只是拿到了 Img 表的字段值,请问该如何拿到外部的值?求指教

    loyalist
        1
    loyalist  
    OP
       May 19, 2020
    已解决。。。使用 django.db.models 中的 OuterRef 即可 ad_id = OuterRef('pk')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2657 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 06:26 · PVG 14:26 · LAX 23:26 · JFK 02:26
    ♥ Do have faith in what you're doing.