查询后django排序字典
django sort dict after query
有一个 table 的网站和一个多对一的 table 的描述
尝试获取列表,首先获取最新的描述,然后按描述的内容排序显示它们...
在views.py
中有以下内容
def category(request, category_name_slug):
"""Category Page"""
context_dict = {}
try:
category = Category.objects.get(slug=category_name_slug)
subcategory = SubCategory.objects.filter(category=category)
websites = Website.objects.filter(sub_categories=subcategory, online=True, banned=False)
sites = websites
descriptions = WebsiteDescription.objects.prefetch_related("about")
descriptions = descriptions.filter(about__in=sites)
descriptions = descriptions.order_by('about', '-updated')
descs = []
last_site = "" # The latest site selected
# Select the first (the latest) from each site group
for desc in descriptions:
if last_site != desc.about.id:
last_site = desc.about.id
desc.url = desc.about.url
desc.hs_id = desc.about.id
desc.banned = desc.about.banned
desc.referral = desc.about.referral
descs.append(desc)
context_dict['descs'] = descs
context_dict['websites'] = websites
context_dict['subcategory'] = subcategory
context_dict['category'] = category
except SubCategory.DoesNotExist:
pass
return render(request, 'category.html', context_dict)
这给了我一个包含站点及其最新描述的列表,所以我在 category.html
中有以下内容
{% if category %}
<h1>{{ category.name }}</h1>
{% for subcategory in category.subcategory_set.all %}
<a href="/links/{{ category.slug }}/{{ subcategory.slug }}">{{ subcategory.name }} ({{ subcategory.website_set.all|length }}) </a>
{% endfor %}
{% if descs %}
{% load endless %}
{% paginate descs %}
{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo" %}
<ul id='list' class='linksteps'>
<a href="/{{ desc.about_id }}" rel="nofollow" target="_blank">
<img src="/static/screenshots/{{ desc.about_id }}.png" />
</a>
<li><h3><a href="/{{ desc.about_id }}" rel="nofollow" target="_blank">{{ desc.about_id }}</a>{% if desc.title %} - {{ desc.title }} {% endif %}</h3>
{% if desc.description %}<b>Description: </b>{{ desc.description }}
<br />{% endif %} {% if desc.subject %}<b>Keywords: </b>{{ desc.subject }}
<br />{% endif %} {% if desc.type %}<b>Type: </b>{{ desc.type }}
<br />{% endif %} {% if desc.officialInfo %} {% if desc.language %}<b>Language: </b>{{ desc.language }}
<br />{% endif %} {% if desc.contactInformation %}<b>Contact info: </b>{{ desc.contactInformation }}
<br />{% endif %}
{% else %}
{% endif %}
</li>
</ul>
</div>
{% endfor %}
{% show_pages %}
{% else %}
<strong>No websites currently in category.</strong>
{% endif %}
{% else %}
The specified subcategory {{ category_name }} does not exist!
{% endif %}
最初我使用dictsort
{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo"|dictsortreversed:"referral" %}
按所需顺序给我列表,所以我很高兴 ;)
然后我决定我需要一些分页,因为列表变得太长了。
django-endless-pagination 工作正常并且也做了它应该做的,但是它在 dictsort 开始之前拆分了我的列表。
有没有一种方法可以在分页发生之前和之后 ordered_by 在初始查询中选择最新的描述?
非常感谢
编辑:
没有得到任何答案,所以我的问题可能不清楚。
据我所知,我需要在 views.py 的末尾对 context_dict 中的值进行排序,替换模板中的字典排序
已解决:::
这样做对我来说是取代dictsort的诀窍。
descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1
已解决:::
这样做对我来说是取代dictsort的诀窍。
descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1
有一个 table 的网站和一个多对一的 table 的描述 尝试获取列表,首先获取最新的描述,然后按描述的内容排序显示它们...
在views.py
中有以下内容def category(request, category_name_slug):
"""Category Page"""
context_dict = {}
try:
category = Category.objects.get(slug=category_name_slug)
subcategory = SubCategory.objects.filter(category=category)
websites = Website.objects.filter(sub_categories=subcategory, online=True, banned=False)
sites = websites
descriptions = WebsiteDescription.objects.prefetch_related("about")
descriptions = descriptions.filter(about__in=sites)
descriptions = descriptions.order_by('about', '-updated')
descs = []
last_site = "" # The latest site selected
# Select the first (the latest) from each site group
for desc in descriptions:
if last_site != desc.about.id:
last_site = desc.about.id
desc.url = desc.about.url
desc.hs_id = desc.about.id
desc.banned = desc.about.banned
desc.referral = desc.about.referral
descs.append(desc)
context_dict['descs'] = descs
context_dict['websites'] = websites
context_dict['subcategory'] = subcategory
context_dict['category'] = category
except SubCategory.DoesNotExist:
pass
return render(request, 'category.html', context_dict)
这给了我一个包含站点及其最新描述的列表,所以我在 category.html
中有以下内容 {% if category %}
<h1>{{ category.name }}</h1>
{% for subcategory in category.subcategory_set.all %}
<a href="/links/{{ category.slug }}/{{ subcategory.slug }}">{{ subcategory.name }} ({{ subcategory.website_set.all|length }}) </a>
{% endfor %}
{% if descs %}
{% load endless %}
{% paginate descs %}
{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo" %}
<ul id='list' class='linksteps'>
<a href="/{{ desc.about_id }}" rel="nofollow" target="_blank">
<img src="/static/screenshots/{{ desc.about_id }}.png" />
</a>
<li><h3><a href="/{{ desc.about_id }}" rel="nofollow" target="_blank">{{ desc.about_id }}</a>{% if desc.title %} - {{ desc.title }} {% endif %}</h3>
{% if desc.description %}<b>Description: </b>{{ desc.description }}
<br />{% endif %} {% if desc.subject %}<b>Keywords: </b>{{ desc.subject }}
<br />{% endif %} {% if desc.type %}<b>Type: </b>{{ desc.type }}
<br />{% endif %} {% if desc.officialInfo %} {% if desc.language %}<b>Language: </b>{{ desc.language }}
<br />{% endif %} {% if desc.contactInformation %}<b>Contact info: </b>{{ desc.contactInformation }}
<br />{% endif %}
{% else %}
{% endif %}
</li>
</ul>
</div>
{% endfor %}
{% show_pages %}
{% else %}
<strong>No websites currently in category.</strong>
{% endif %}
{% else %}
The specified subcategory {{ category_name }} does not exist!
{% endif %}
最初我使用dictsort
{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo"|dictsortreversed:"referral" %}
按所需顺序给我列表,所以我很高兴 ;) 然后我决定我需要一些分页,因为列表变得太长了。 django-endless-pagination 工作正常并且也做了它应该做的,但是它在 dictsort 开始之前拆分了我的列表。
有没有一种方法可以在分页发生之前和之后 ordered_by 在初始查询中选择最新的描述?
非常感谢
编辑:
没有得到任何答案,所以我的问题可能不清楚。
据我所知,我需要在 views.py 的末尾对 context_dict 中的值进行排序,替换模板中的字典排序
已解决:::
这样做对我来说是取代dictsort的诀窍。
descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1
已解决:::
这样做对我来说是取代dictsort的诀窍。
descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1