G
G
Grigory Dikiy2017-05-07 13:25:30
Django
Grigory Dikiy, 2017-05-07 13:25:30

Django mptt getting parents?

Good afternoon! I created a tree menu using mptt, but when implementing bread cats, I came across a problem: I can’t display all parents. Actually the code itself:

class CategoryView(views.IndexView):
    """
    View: Страница категории
    """
    def get_context_data(self, **kwargs):
        category = get_object_or_404(Category, slug=self.kwargs['slug'], id=self.kwargs['id'])
        products = Product.objects.filter(category__slug=self.kwargs['slug'])
        metatags = create_metatags(category.name)
        return {
            'products': products,
            'category': category,
            'breadcrumb': get_breadcrumb(category),
            'metatags': metatags,
            'section': 'menu/product_category.html'
        }
....
def get_breadcrumb(category):
    breadcrumb = []
    for parent in category.get_ancestors(ascending=True, include_self=True):
        breadcrumb.append({
            'url': parent.get_absolute_url(),
            'title': parent.name
        })

    return breadcrumb

In the admin panel, the category structure is as follows:
3f09c6b081c4465bb162de35f8971e06.png
And in the end I get only this:
b7bed5c9387c4e219363dc51d7d151b1.png
Although it should be like this:
Home / Chandeliers / Classic chandeliers
Actually the question is: what am I doing wrong?
PS. I work with mptt for the first time, please do not throw stones

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-05-07
@frilix

Well, Chandeliers do not have a descendant Classic chandeliers
Drag-and-drop Classic chandeliers under Chandeliers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question