V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
HashV2
V2EX  ›  Python

求教一个 Django 的 ORM 查询怎么写

  •  
  •   HashV2 · 2021-07-02 10:41:43 +08:00 · 1950 次点击
    这是一个创建于 1000 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有两张表分别记录了一个对象,和对象所发生的变动线, 我以鼠标举个例子

    • 可能不是那么恰当,总之就是记录一个流程哈 这里流程并不是一直往下走 比如审批不通过 再研发去*
    • 我想要查询某个时间段,经历过审批不通过开模的所有鼠标,可以 ORM 写吗?原生 SQL 也行
    class Mouse(models.Model):
        name = models.CharField('鼠标型号 /名称', max_length=255)
        ...
    
    CHOICE = (
        (1, '立项'),
        (2, '设计'),
        (3, '研发'),
        (4, '审批'),
        (5, '审批不通过'),
        (6, '审批通过'),
        (7, '开模'),
        ...
    )
    
    class MouseRel(models.Model):
        name = models.PositiveIntegerField('变动名称',choice=CHOICE)
        time = models.DateTimeField('变动时间', auto_now_add=True)
        mouse = models.Foreignkey(
            Mouse,
            db_index=True,
            on_delete=models.CASCADE,
            related_name='rels'
        )
        ...
    
    14 条回复    2021-07-03 15:49:30 +08:00
    xixijun
        1
    xixijun  
       2021-07-02 10:57:22 +08:00   ❤️ 1
    MouseRel.objects.filter(mouse__in=(5,7), time__lt='2021-07-02',time__gt='2021-07-01')
    不知道理解的对不对。
    xixijun
        2
    xixijun  
       2021-07-02 10:58:38 +08:00
    mouse__in -> mouse_id__in
    leebx
        3
    leebx  
       2021-07-02 11:00:19 +08:00   ❤️ 1
    MouseRel.objects.filter(time__range=(starttime,endtime)).filter(Q(name=5) | Q(name=7))
    HashV2
        4
    HashV2  
    OP
       2021-07-02 11:02:13 +08:00
    @xixijun #1
    @leebx #3 这查的都是 MouseRel 吧 我想查的是 Mouse
    leebx
        5
    leebx  
       2021-07-02 11:08:43 +08:00
    @HashV2 mr= MouseRel.objects.filter(time__range=(starttime,endtime)).filter(Q(name=5) | Q(name=7)) mouses = [m.mouse.name for m in mr]
    HashV2
        6
    HashV2  
    OP
       2021-07-02 11:15:56 +08:00
    @leebx #5 一个 mouse 会关联很多 mouserel 这种会查出来很多重复的 mouse 还要自己去 distinct, 我是希望是能直接从 Mouse.objects.filter(query_params).distinct() 直接查出相关 mouse 数据
    leebx
        7
    leebx  
       2021-07-02 11:23:08 +08:00
    @HashV2 Mouse.mouserel_set.filter(time__range=(starttime,endtime)).filter(Q(name=5) | Q(name=7))
    jabari
        8
    jabari  
       2021-07-02 11:34:22 +08:00   ❤️ 3
    ```
    Mouse.objects.filter(rels__name__in=[5, 7], rels__time__range=(start_time, end_time)).distinct()
    ```
    dicc
        9
    dicc  
       2021-07-02 11:57:46 +08:00
    django 聚合查询 具体的语句就不说了
    dicc
        10
    dicc  
       2021-07-02 11:59:09 +08:00
    @jabari 学到了学到了,
    HashV2
        11
    HashV2  
    OP
       2021-07-02 12:20:51 +08:00
    @jabari #8 正解 感谢
    leven87
        12
    leven87  
       2021-07-02 16:36:46 +08:00
    吐槽一下,同样在写 django orm, 以前没写过,感觉真是坑多的要死。
    wangchonglie
        13
    wangchonglie  
       2021-07-02 16:51:51 +08:00
    @leven87 #12 同意, Django 的 ORM 并不好用
    l4ever
        14
    l4ever  
       2021-07-03 15:49:30 +08:00
    还是 flask 好, 自己随便折腾.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3330 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 13:29 · PVG 21:29 · LAX 06:29 · JFK 09:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.