Python Django从get_queryset(self)排序搜索结果
我正在使用django 2.2.10。我有一个搜索栏,它使用searchresultsview(listview)类中的get_queryset(self)将搜索结果返回到showpage。我已将paginate_设置为=10。在前端,我制作了链接来订购表格:`Python Django从get_queryset(self)排序搜索结果,python,html,django,Python,Html,Django,我正在使用django 2.2.10。我有一个搜索栏,它使用searchresultsview(listview)类中的get_queryset(self)将搜索结果返回到showpage。我已将paginate_设置为=10。在前端,我制作了链接来订购表格:` 标题原件 在get_queryset(self)函数的开头,我有以下代码: order_by = self.request.GET.get('order_by') direction = self.req
标题原件
在get_queryset(self)函数的开头,我有以下代码:
order_by = self.request.GET.get('order_by')
direction = self.request.GET.get('direction')
if order_by is not None and order_by != "" and direction is not None and direction != "":
ordering = Lower(order_by)
if direction == 'desc':
ordering = '-{}'.format(ordering)
publications = Publication.objects.filter(is_deleted=False).order_by(ordering)
'''
paginator = Paginator(publications, 10)
page = self.request.GET.get('page')
try:
all_publications = paginator.page(page)
except PageNotAnInteger:
all_publications = paginator.page(1)
except EmptyPage:
all_publications = paginator.page(paginator.num_pages)
'''
return publications
主要问题是publications变量包含所有发布,我想将其限制为上一个get_queryset(self)调用中的发布。此外,showpage已分页(模板中的分页方式=10)。降序顺序不起作用。我得到:参数的降序无效:['-Lower(F(title_original))']。如果我按升序排列,它在逻辑上不会保留我的搜索结果。
我试图找到stackoverflow解决方案,但解释很少。任何帮助都将不胜感激
使用django表可能更容易,如果是的话,我愿意听取建议
查看代码:
class SearchResultsView(ListView):
'''
ListView of the initial search page.
The function get_queryset works for the search bar and the search form home page.
The search bar typically uses q for query otherwise a id for list search.
Use a countries_dict to convert for example Netherlands to NL so that search succeeds.
If a normal field is searched use __icontains if a list element is searched use: __in.
'''
model = Publication
template_name = 'publications/show.html'
context_object_name = 'publications'
publications = Publication.objects.filter(is_deleted=False)
#paginator = Paginator(publications, 10)
#paginator = Paginator(publications, 25)
paginate_by = 10
def get_ordering(self):
order_by = self.request.GET.get('order_by')
direction = self.request.GET.get('direction')
if order_by is not None and order_by != "" and direction is not None and direction != "":
ordering = Lower(order_by)
if direction == 'desc':
ordering = '-{}'.format(ordering)
return ordering
def get_queryset(self):
#form = PublicationForm(self.request.GET)
authors = self.request.GET.getlist('author')
translators = self.request.GET.getlist('translator')
authors = Author.objects.filter(pk__in=authors).all()
translators = Translator.objects.filter(pk__in=translators).all()
form_of_publications = self.request.GET.getlist('form_of_publication')
form_of_publications = FormOfPublication.objects.filter(pk__in=form_of_publications).all()
languages = self.request.GET.getlist('language')
languages = Language.objects.filter(pk__in=languages).all()
affiliated_churches = self.request.GET.getlist('affiliated_church')
affiliated_churches = Church.objects.filter(pk__in=affiliated_churches).all()
content_genres = self.request.GET.getlist('content_genre')
content_genres = Genre.objects.filter(pk__in=content_genres).all()
connected_to_special_occasions = self.request.GET.getlist('connected_to_special_occasion')
connected_to_special_occasions = SpecialOccasion.objects.filter(pk__in=connected_to_special_occasions).all()
currently_owned_by = self.request.GET.getlist('currently_owned_by')
currently_owned_by = Owner.objects.filter(pk__in=currently_owned_by).all()
copyrights = self.request.GET.get('copyrights')
is_a_translation = self.request.GET.get('is_a_translation')
publications = Publication.objects.filter(is_deleted=False)
uploadedfiles = self.request.GET.getlist('uploadedfiles')
uploadedfiles = UploadedFile.objects.filter(pk__in=uploadedfiles).all()
keywords = self.request.GET.getlist('keywords')
keywords = Keyword.objects.filter(pk__in=keywords).all()
translated_from = self.request.GET.getlist('translated_From')
translated_from = Language.objects.filter(pk__in=translated_from).all()
city = self.request.GET.getlist('publication_city')
country = self.request.GET.getlist('publication_country')
collection_country = self.request.GET.getlist('collection_country')
if list(collection_country) != ['']:
collection_country = Country.objects.filter(pk__in=city).all()
if list(country) != ['']:
country = Country.objects.filter(pk__in=city).all()
print('....', city)
if list(city) != ['']:
city = City.objects.filter(pk__in=city).all()
print(publications)
exclude = ['csrfmiddlewaretoken','search']
in_variables = [('author', authors), ('translator', translators), ('form_of_publication', form_of_publications), ('language',languages), ('affiliated_church', affiliated_churches) \
, ('content_genre', content_genres), ('connected_to_special_occasion', connected_to_special_occasions), ('currently_owned_by', currently_owned_by),\
('uploadedfiles', uploadedfiles), ('publication_country', country), ('publication_city', city), ('collection_country', collection_country), ('keywords', keywords), ('translated_from',translated_from)]
special_case = ['copyrights', 'page', 'is_a_translation']
if ('q' in self.request.GET) and self.request.GET['q'].strip():
query_string = self.request.GET['q']
if query_string.lower() in countries_dict.keys():
query_string = countries_dict[query_string.lower()]
search_fields = ['title_original', 'title_subtitle_transcription', 'title_subtitle_European', 'title_translation', 'author__name', 'author__name_original_language', 'author__extra_info', \
'form_of_publication__name', 'editor', 'printed_by', 'published_by', 'publication_date', 'publication_country__name', 'publication_city__name', 'publishing_organisation', 'translator__name', 'translator__name_original_language', 'translator__extra_info', \
'language__name', 'language__direction', 'affiliated_church__name', 'extra_info', 'content_genre__name', 'connected_to_special_occasion__name', 'donor', 'content_description', 'description_of_illustration', \
'nr_of_pages', 'collection_date', 'collection_country__name', 'collection_venue_and_city', 'contact_telephone_number', 'contact_email', 'contact_website', \
'currently_owned_by__name', 'uploadedfiles__description', 'uploadedfiles__uploaded_at', 'general_comments', 'team_comments', 'other_comments', 'keywords__name', 'is_a_translation', 'ISBN_number', 'translated_from__name', 'translated_from__direction']
arabic_query = translator.translate(query_string, dest='ar').text
query_string = to_searchable(query_string)
#arabic_query = to_searchable(arabic_query)
entry_query = get_query(query_string, search_fields)
arabic_query = get_query(arabic_query, search_fields)
print('&&&&&&', query_string)
#publications = publications.filter(entry_query)
publications = publications.filter(Q(entry_query) | Q(arabic_query))
print(publications)
publications = publications.distinct()
return publications
for field_name in self.request.GET:
get_value = self.request.GET.get(field_name)
if get_value != "" and not field_name in exclude and not field_name in [i[0] for i in in_variables] and\
not field_name in special_case:
print('******', field_name)
arabic_query = translator.translate(get_value, dest='ar').text
get_value = to_searchable(get_value)
get_value = get_query(get_value, [field_name])
arabic_query = get_query(arabic_query, [field_name])
print('444444444', get_value)
publications = publications.filter(Q(get_value) | Q(arabic_query))
print('55555555555', publications)
#publications = publications.filter(Q(**{field_name+'__regex':get_value}) | Q(**{field_name+'__icontains':arabic_query}) )
for field_name, list_object in in_variables:
print('****', list_object)
if list_object:
print('------', field_name)
if list(list_object) != ['']:
publications = publications.filter(**{field_name+'__in': list_object})
if str(copyrights) != "unknown" and str(copyrights) != "None":
val = False
if str(copyrights) == "yes":
val = True
print('11111', str(copyrights))
publications = publications.filter(copyrights=val)
print('666666', publications)
if str(is_a_translation) != "unknown" and str(is_a_translation) != "None":
val = False
if str(is_a_translation) == "yes":
val = True
print('11111', str(is_a_translation))
publications = publications.filter(is_a_translation=val)
publications = publications.distinct()
return publications
您可以使用
get\u ordering
方法
def get_ordering(self):
ordering = self.request.GET.get('ordering', ''#default order param)
return ordering
我解决了这个问题。基本上,如果您有一个get_queryset方法,您需要从那里调用get_ordering。此外,对于分页,您需要在上下文中设置排序变量,这样,例如,如果您转到第2页,将保持排序。以下是解决方案代码:
ordering = self.get_ordering()
if ordering is not None and ordering != "":
publications = publications.order_by(ordering)
return publications
def get_context_data(self, **kwargs):
context = super(SearchResultsView, self).get_context_data(**kwargs)
order_by = self.request.GET.get('order_by')
if order_by is not None and order_by != "":
context['order_by'] = order_by
context['direction'] = self.request.GET.get('direction')
else:
context['order_by'] = ''
context['direction'] = ''
q = self.request.GET.get('q')
if q is not None and q != "":
context['q'] = q
else:
context['q'] = ''
return context
以及html代码:
{% extends "base.html" %}
{% block content %}
<table class="table table-striped table-bordered table-sm">
<thead class="thead-light">
<tr>
<th>Title original <a href="?q={{ q }}&order_by=title_original&direction=asc" class="arrow up"></a> <a href="?q={{ q }}&order_by=title_original&direction=desc" class="arrow down"></a></th>
<th>Title subtitle transcription<a href="?q={{ q }}&order_by=title_subtitle_transcription&direction=asc" class="arrow up"></a> <a href="?q={{ q }}&order_by=title_subtitle_transcription&direction=desc" class="arrow down"></a></th>
<th>Title translation<a href="?q={{ q }}&order_by=title_translation&direction=asc" class="arrow up"></a> <a href="?q={{ q }}&order_by=title_translation&direction=desc" class="arrow down"></a></th></th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<br />
{% for publication in publications %}
<tr id="publications">
<td style="text-align: start;unicode-bidi: plaintext;">{{ publication.title_original }}</td>
<td>{{ publication.title_subtitle_transcription}}</td>
<td>{{ publication.title_translation }}</td>
<td>
<a href="/publication/{{ publication.id }}/detail_view">View</a>
<a href="/publication/{{ publication.id }}/edit"><span class="glyphicon glyphicon-pencil" >Edit</span></a>
<a class="confirm-delete-pub" href="/publication/{{ publication.id }}/delete">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?q={{ q }}&page=1&order_by={{order_by}}&direction={{ direction }}">« first</a>
<a href="?q={{ q }}&page={{ page_obj.previous_page_number }}&order_by={{order_by}}&direction={{ direction }}">previous</a>
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?q={{ q }}&page={{ page_obj.next_page_number }}&order_by={{order_by}}&direction={{ direction }}">next</a>
<a href="?q={{ q }}&page={{ page_obj.paginator.num_pages }}&order_by={{order_by}}&direction={{ direction }}">last »</a>
{% endif %}
</span>
</div>
{% endblock %}
{%extends“base.html”%}
{%block content%}
标题原件
标题字幕转录
标题翻译
行动
{%用于在发布%中发布}
{{publication.title_original}}
{{出版物.标题{字幕}
{{publication.title_translation}
{%endfor%}
{%如果页面_obj.has_previous%}
{%endif%}
第{{Page_obj.paginator.num_pages}页中的第{{Page_obj.number}页。
{%如果页面_obj.has_next%}
{%endif%}
{%endblock%}
我已将我的代码放入get_订购(self)中。但是,它仍然运行get_queryset(self)方法。我需要向URL.py添加一些内容吗?我需要上升和下降。我仍然称它们为“订购方式和方向”。您必须从get_queryset中删除订购代码。您还可以使用-
参数管理方向。我的意思是,您可以在url上发送带有-
的订购参数。我在get\u queryset中没有订购代码。我使用负号就像上面的代码一样,应该足够了吗?我没有像上面那样使用“订购”而是“订购人”和“方向”。这不要紧,因为这不是必需的。你可以随心所欲地设计。仅限于,您必须更改参数名称。重要的是:您必须从这个get\u ordering
方法返回一个有效的字段名。我仍然得到这个:FieldError at/无法将关键字'order\u by'解析到字段中。并引用get_queryset()