查询后django排序字典

时间:2016-02-12 16:26:14

标签: python django django-endless-pagination

有一个包含网站的表格和一个包含说明的多对一表格 试图获取一个列表,首先获取最新的描述,然后按描述的内容显示它们......

在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

解决:::

这样做可以让我替换dictsort。

descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1

1 个答案:

答案 0 :(得分:0)

解决:::

这样做可以让我替换dictsort。

descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1