Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:43:17+00:00 2026-06-08T17:43:17+00:00

I am working with django-mptt and I’m stuck. I want to get all objects

  • 0

I am working with django-mptt and I’m stuck.

I want to get all objects for each category with all objects from descendant categories.

I managed to achieve my goal but there is one problem – I won’t be able to sort items because they are generated from multiple objects.

I suppose that my approach is bad.

How I can achive my goal and make it possible to combine all returned objects and order them by date?

View:

 def category_view(request, pk, slug, sub_pk=None, subcategory=None):

    if not subcategory:
        current_category = get_object_or_404(Category, pk=int(pk))
        adv_obj = current_category.get_descendants(include_self=True)
        print adv_obj

    else:
        current_category = get_object_or_404(Category, pk=int(sub_pk))
        adv_obj = current_category.get_descendants(include_self=True)


    return TemplateResponse(request, "category_view.html", {'category_details':current_category,
                                                            'advert': adv_obj,
                                                           },
                            )

Template:

{% block content %}
<div class="grid_8">
    <h2>{{category_details.name}}</h2>
    <hr>
    <div>
        {% for n in advert.all %}
        {% for p in n.advert_set.all %}

        <div>
            <h3>{{p.title}}</h3>
            <span class="annoucement_detail">Kategoria: </span><a href="{{n.category.get_absolute_url}}">{{p.category.name}}</a>
            {% if p.location %} | <span class="annoucement_detail">Miejscowość: </span>{{p.location}}
            {% endif %}
            <div>
                <span class="annoucement_detail">Data dodania: </span> {{p.date_added}} | <span class="annoucement_detail">Data wygaśnięcia: </span>{{p.expiration_date}}
            </div>
            <div>
                {{p.text}}
            </div>
        </div>
        <hr>
        {% endfor %}
        {% endfor %}
    </div>

</div>
{% endblock%}

Edit:

I came up with an idea. I’ll iterate over results in a view and chain it with itertools.
I will check tomorrow if it’s going to work 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:43:19+00:00Added an answer on June 8, 2026 at 5:43 pm

    Thats my anwser 🙂

    def category_view(request, pk, slug, sub_pk=None, subcategory=None):
    
        advert_list = []
    
        if not subcategory:
            current_category = get_object_or_404(Category, pk=int(pk))
            adv_obj = current_category.get_descendants(include_self=True)
    
            for n in adv_obj:
    
                for p in n.advert_set.all().order_by('-date_added'):
                    advert_list.append(p)
    
            adv_obj = chain(advert_list)
    
            adv_obj = sorted(adv_obj, key=operator.attrgetter('date_added'))
            adv_obj.reverse()
    
    
        else:
            current_category = get_object_or_404(Category, pk=int(sub_pk))
            adv_obj = current_category.get_descendants(include_self=True)
    
            for n in adv_obj:
    
                for p in n.advert_set.all().order_by('-date_added'):
                    advert_list.append(p)
    
            adv_obj = chain(advert_list)
            adv_obj = sorted(adv_obj, key=operator.attrgetter('date_added'))
            adv_obj.reverse()
    
        return TemplateResponse(request, "category_view.html", {'category_details':current_category,
                                                                'advert': adv_obj,
                                                               },
                                )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am working with django Publisher example, I want to list all the publishers in
I'm currently working on django tagging . I want to retrieve all the entries
I'm working in Django, and using urllib2 and simplejson to parse some information from
I'm working on a django-nonrel project running on Google's AppEngine. I want to create
I'm working with django, i need send a mail to many emails, i want
I'm working on debugging Django from the command-line. I'm working with django_extensions , and
I am working on Django application where users can exchange post-it notes with each
I have been trying to get xapian working django haystack for a project im
What are the steps for installing all the dependencies needed to get python django
I've been working with Django in order to make my portfolio and I've managed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.