A
A
AlexandrBirukov2018-09-03 16:55:15
Django
AlexandrBirukov, 2018-09-03 16:55:15

How to filter MPTTModel by related models?

There are models:

class Place(MPTTModel):
    name = models.CharField(_('name'), max_length=200)
    parent = TreeForeignKey(
        'self', null=True, blank=True, related_name='children',
        verbose_name=_('parent'), db_index=True, on_delete=models.CASCADE
    )

class Product(models.Model):
    category = TreeForeignKey(
        Category, related_name='products', verbose_name=_('category'), db_index=True, on_delete=models.CASCADE
    )
    name = models.CharField(_('name'), max_length=200)
    place = TreeForeignKey(
        Place, related_name='products', verbose_name=_('product place'), db_index=True, on_delete=models.CASCADE
    )

I'm trying to get all the places for products from a particular category, something like:
places = Place.objects.filter(products__category=self.category)

but I get an error in the template Node class 'modeltranslation.manager.multilingual_queryset_factory.locals.NewClass' not in depth-first order
I understand that by filtering I break the mptt tree and cannot display it in the template. Who knows, please write how to properly filter or display the result in a template, thanks in advance)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question