Django select_related filter

女生的网名这么多〃 提交于 2019-12-21 03:42:05

问题


I have the following Django models.

class A(models.Model):
    tmp = models.ForeignKey(B)
    active = models.BooleanField()

class B(models.Model):
    active = models.BooleanField()
    archived = models.BooleanField()

Now I have the following query.

A.objects.select_related(B).filter(active=True)

Now this fetches all the objects of B. Now how can I include a filter of active=True and archived=False in the select_related clause for model B.


回答1:


The same as you would with any other related field, with a __ lookup..

A.objects.select_related(B).filter(active=True, tmp__active=True, tmp__archived=False)

Using select related doesn't change anything here, its purpose is about what information is returned with the results, it has no effect on filtering at all.



来源:https://stackoverflow.com/questions/40926898/django-select-related-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!