1

have a table with websites and a many to one table with descriptions trying to get a list, firstly getting the latest descriptions and then displaying them ordered by the content of the descriptions...

have the following in 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)

this gives me a list with sites and their latest descriptions, so i have the following in 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 %}

Initially i used dictsort

{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo"|dictsortreversed:"referral" %}

to give me the list in the desired order, so i was all happy ;) Then however i decided i needed some pagination because the lists became too long. django-endless-pagination works fine and does what its supposed too, however it splits up my list before the dictsort kicks in.

is there a way to sort before pagination happens and after i ordered_by at the initial query to have the latest descriptions selected?

much obliged

EDIT:

not getting any answers so my question might not be clear.

as far as i understand i need to sort the values in context_dict at the end in views.py replacing the dictsort as in the template

SOLVED:::

doing this did the trick for me to replace the dictsort.

descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1
Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
adamzia
  • 45
  • 7
  • You should sort you data in the view, and not in the template. Not only will it make it possible to paginate, but it's also way faster. – Mark Chackerian Feb 12 '16 at 16:28
  • i was already happy with a result after i tried that initially, my django-fu simply not good enough and dictsort gave a quick result, can' t get the part where i re-order after the initial order in the view – adamzia Feb 12 '16 at 16:42
  • @MarkChackerian , so the question is how to sort it in the view? the closest way i can find would be using something like OrderedDict, however i can not seem to get the values right i guess, if that is the way to go at all – adamzia Feb 23 '16 at 10:35
  • 1
    I'm not sure exactly how you are trying to sort, but it's usually best to do your sorting in the database if possible -- that gives you more control for pagination, and can be much faster if the database is indexed properly. You can sort across joined models -- see the example in https://docs.djangoproject.com/en/1.9/ref/models/querysets/#order-by Also, there are some general tips here http://stackoverflow.com/questions/2412770/good-ways-to-sort-a-queryset-django – Mark Chackerian Feb 23 '16 at 16:20
  • yes, order_by is how i usually do it, but unless i'm very much mistaking here and also explained in the question, the problem is that i first sort by date to get the latest from the descriptions in the many to many field.... tried the ordered = sorted as well, but remember that not working, will try that again however. – adamzia Feb 23 '16 at 17:04
  • thank you good sir, that works ;) had used itemgetter before and that give errors – adamzia Feb 23 '16 at 17:33
  • @adamzia Don't add the solution to the question. Instead, post it as an answer and mark your answer as accepted. – Vincent Savard Feb 23 '16 at 17:42

1 Answers1

0

SOLVED:::

doing this did the trick for me to replace the dictsort.

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