Y
Y
Yzurgzd2020-10-30 10:47:13
Django
Yzurgzd, 2020-10-30 10:47:13

How to pass self to filter for Django views?

models

class Product(models.Model):
    article = models.CharField('Артикул', max_length=100)
    name = models.CharField('Наименование', max_length=100)
    poster = models.ImageField('Постер', upload_to='products/posters/')
    description = models.TextField('Описание')
    category = models.ForeignKey(
        Category, verbose_name='Категория', on_delete=models.SET_NULL, null=True)
    price = models.PositiveIntegerField(
        'Цена', default=0, help_text='Указывать сумму в рублях')
    hide = models.BooleanField('Скрыть', default=False)
    slug = models.SlugField(max_length=160, unique=True)


views
class ProductDetailView(DetailView):
    module = Product
    queryset = Product.objects.filter(hide=False)
    template_name = 'products/single-product.html'

    def get_last_products(self):
        return Product.objects.filter(category=self.product.category) #Вот ТУТ


In the template for ProductDetail I output through a loop How do I pass the category from product to get_last_products? {% for last_product in view.get_last_products %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-10-30
@Yzurgzd

Well, definitely not how to do the view method, either do it as a model method, or pass it to the template as a context via get_context_data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question