0

我正在使用 django-mptt 并且我被卡住了。

我想获取每个类别的所有对象以及后代类别的所有对象。

我设法实现了我的目标,但有一个问题 - 我将无法对项目进行排序,因为它们是从多个对象生成的。

我想我的方法很糟糕。

我怎样才能实现我的目标,并可以组合所有返回的对象并按日期排序?

看法:

 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,
                                                           },
                            )

模板:

{% 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%}

编辑:

我想出了一个主意。我将在视图中迭代结果并使用 itertools 将其链接起来。我明天会检查它是否可以工作:)

4

1 回答 1

1

那是我的回答:)

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,
                                                           },
                            )
于 2012-07-29T09:50:06.180 回答