本文介绍了SearchQuerySet的哪个属性与prefetch_related具有相同的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


binding_query = query_parameters ['binding_query']
query_parameters ['validate'] = 1

def get_books_by_query_params(context,query,query_parameters) default_query = None
如果查询:
default_queries = [
Q(title__icontains = query),
Q(isbn_10__contains = query),
Q(isbn_13__contains = query)
Q(publishers = Publisher.objects.filter(name = query)),
Q(Institutes = Institute.objects.filter(name = query)),
Q(authors = Author。 object.filter(name = query)),
Q(sellers = Seller.objects.filter(name = query))
]

default_query = reduce(operator.or_, default_queries)
default_query = default_query& binding_query如果binding_query不是无其他default_query
elif binding_query不是无:
default_query = binding_query

如果default_query不为None,而query_parameters ['query_parameters']不为None:
books = Book.objects.filter(default_query,validate = 1,** query_parameters ['query_parameters'])。distinct()\
.prefetch_related('authors')。prefetch_related('publishers') .prefetch_related('sellerers)。prefetch_related('institutes')
elif query_parameters ['query_parameters']:
books = Book.objects.filter(validate = 1,** query_parameters ['query_parameters'] ).distinct()\
.prefetch_related('authors')。prefetch_related('publishers')。prefetch_related('sellerers)。prefetch_related('institutes')
elif default_query:
books = Book.objects.filter(default_query,validate = 1).distinct()。prefetch_related('authors')。prefetch_related('publishers')。prefetch_related ('seller')。prefetch_related('institutes')
else:
books = Book.objects.filter(validate = 1).distinct()。prefetch_related('authors')。prefetch_related('publishers ').prefetch_related('sellerers)。prefetch_related('institutes')

上下文['books'] = books
return context
/ pre>

如何使用searchqueryset编写以下代码?在这种情况下可以使用的searchqueryset中有相当于 prefetch_related

解决方案

filter_and


def get_books_by_query_params(context, query, query_parameters):
binding_query = query_parameters['binding_query']
query_parameters['validate']=1
default_query = None
if query:
    default_queries = [
        Q(title__icontains=query),
        Q(isbn_10__contains=query),
        Q(isbn_13__contains=query),
        Q(publishers=Publisher.objects.filter(name=query)),
        Q(institutes=Institute.objects.filter(name=query)),
        Q(authors=Author.objects.filter(name=query)),
        Q(sellers=Seller.objects.filter(name=query))
    ]

    default_query = reduce(operator.or_, default_queries)
    default_query = default_query & binding_query if binding_query is not None else default_query
elif binding_query is not None:
    default_query = binding_query

if default_query is not None and query_parameters['query_parameters'] is not None:
    books = Book.objects.filter(default_query, validate=1, **query_parameters['query_parameters']).distinct()\
        .prefetch_related('authors').prefetch_related('publishers').prefetch_related('sellers').prefetch_related('institutes')
elif query_parameters['query_parameters']:
    books = Book.objects.filter(validate=1,**query_parameters['query_parameters']).distinct()\
        .prefetch_related('authors').prefetch_related('publishers').prefetch_related('sellers').prefetch_related('institutes')
elif default_query:
    books = Book.objects.filter(default_query,validate=1).distinct().prefetch_related('authors').prefetch_related('publishers').prefetch_related('sellers').prefetch_related('institutes')
else:
    books = Book.objects.filter(validate=1).distinct().prefetch_related('authors').prefetch_related('publishers').prefetch_related('sellers').prefetch_related('institutes')

context['books'] = books
return context

How to write the following code using searchqueryset? Is there an equivalent of the prefetch_related in the searchqueryset that can be used in this case?

解决方案

There is filter_and:

这篇关于SearchQuerySet的哪个属性与prefetch_related具有相同的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:06