Question

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 :)

Was it helpful?

Solution

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,
                                                           },
                            )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top