Answer the question
In order to leave comments, you need to log in
How to implement Filter in models.ForeignKey Django?
models
class Product(models.Model):
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)
class TypeSpecification(models.Model):
category = models.ForeignKey(
Category, verbose_name='Категория', null=True, on_delete=models.SET_NULL)
name = models.CharField('Наименование', max_length=255)
class ProductSpecification(models.Model):
product = models.ForeignKey(
Product, verbose_name='Товар', on_delete=models.CASCADE)
type_specification = models.ForeignKey(
TypeSpecification, verbose_name='Тип характеристик', on_delete=models.CASCADE, limit_choices_to={'category': self.product.category})
value = models.CharField('Значение', max_length=255)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question