Доброе время суток. Подскажите молодому джангисту. Суть вопроса : требуется вывести объекты всех 'детей' в 'родителе'.
models.py
class Category(MPTTModel):
"""
Описание модели категории товара
"""
parent = TreeForeignKey(u'self', verbose_name=_(u'Parent'), blank=True, null=True, related_name=u'children')
name = models.CharField(_('Name'), max_length=255, db_index=True)
description = models.TextField(_('Description'), blank=True)
slug = AutoSlugField(populate_from='name', slugify=custom_slugify, unique=True)
is_active = models.BooleanField(verbose_name=_('Is active'), default=None)
class Product(models.Model):
"""
Описание модели Product
"""
category = TreeForeignKey(Category, verbose_name=_('Category'), related_name=u'entries')
user_name = models.ForeignKey(User, related_name='+', to_field=u'username')
title = models.CharField(_('Product title'), max_length=255, blank=True)
slug = AutoSlugField(populate_from='title', slugify=custom_slugify, unique=False)
price = models.IntegerField(_('Price'), blank=True)
description = models.TextField(_('Description'), blank=True)
date_created = models.DateTimeField(_("Date created"), auto_now_add=True)
date_updated = models.DateTimeField(_("Date updated"), auto_now=True, db_index=True)
view.py
@render_to('apps/catalogue/products_in_category.html')
def products_in_category(request, slug):
current_category = get_object_or_404(Category, slug=slug)
# фильтрация продуктов определенной категории
category_vivod = Product.objects.filter(category_id=current_category.pk)
return dict(category_vivod=category_vivod)
@render_to('apps/catalogue/all_products_in_category.html')
def all_products_in_root_category(request, slug):
current_category = get_object_or_404(Category, slug=slug)
# вывод всех продуктов категории
test1 = current_category.get_children()
test = test1.values('id')
x = [item['id'] for item in test] # получаю список из нужных id - (4, 2)
return dict(x=x )
Подскажите пожалуйста, как сделать QuerySet типа -Product.filter(category_id=x). Или нужно циклом подставлять значения из х в фильтре. Или на уровне метода модели. А может реализация в шаблоне ?Доку mptt изучаю - может ответ там?