difference between filter with multiple arguments and chain filter in django

前端 未结 9 801
[愿得一人]
[愿得一人] 2020-11-30 20:08

What is the difference between filter with multiple arguments and chain filter in django?

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 20:55

    Most of the time, there is only one possible set of results for a query.

    The use for chaining filters comes when you are dealing with m2m:

    Consider this:

    # will return all Model with m2m field 1
    Model.objects.filter(m2m_field=1) 
    
    # will return Model with both 1 AND 2    
    Model.objects.filter(m2m_field=1).filter(m2m_field=2) 
    
    # this will NOT work
    Model.objects.filter(Q(m2m_field=1) & Q(m2m_field=2))
    

    Other examples are welcome.

提交回复
热议问题