如何组合icontains过滤器,以便同时搜索一个字段和另一个字段

时间:2019-03-05 06:20:43

标签: python django filtering

class ThreadManager : IThreadManager
{
    private System.Threading.ManualResetEvent _Mre;
    private static CancellationTokenSource _CancellationToken;
    private int _ThreadCount;

    public ThreadManager(int threadCount)
    {
        _Mre = new System.Threading.ManualResetEvent(true);
        _CancellationToken = new CancellationTokenSource();
        _ThreadCount = threadCount;
    }

    public void DoWork(Action action)
    {
        _Mre.WaitOne();
        Task.Factory.StartNew(action, _CancellationToken.Token);
    }

    public void Stop()
    {
        _CancellationToken.Cancel();
    }

    public void Resume()
    {
        _Mre.Set();
    }

    public void Waite()
    {
        _Mre.Reset();
    }
}

我希望能够同时搜索2件事。例如,如果描述中包含“ big”且def search(request): queryset_list = Listing.objects.order_by('-list_date').filter(is_published=True) if 'keywords' in request.GET: keywords = request.GET['keywords'] if keywords: queryset_list = queryset_list.filter(description__icontains=keywords) if keywords: queryset_list = queryset_list.filter(realtor__name__icontains=keywords) 是否不存在,我想仍然看到realtor__name具有大而相同的对象,即使描述中没有它我也希望得到它。 谢谢

1 个答案:

答案 0 :(得分:1)

好像您需要or运算符

例如:

from django.db.models import Q


if keywords:
    queryset_list = queryset_list.filter(Q(description__icontains=keywords) |
                                         Q(realtor__name__icontains=keywords))