M
M
maxell2019-09-09 20:13:11
Django
maxell, 2019-09-09 20:13:11

How to display all information from the first table?

There are 2 classes: Product (Item) and Product Type (ItemType). I linked them with 'ForeignKey'.
I want that when choosing one category of product, all products of this category are displayed (I make a filter for an online store)
Questions:
1) Have I linked the two tables correctly?
2) How to make a request to display all products of one category
Code:

#models.py
class Item(models.Model):

    item_title = models.CharField('Название', max_length=200)
    item_description = models.TextField('Описание')
    item_size = models.CharField('Размер', max_length=10)
    item_price = models.IntegerField('Цена')
    item_pub_date = models.DateTimeField('Дата публицкации', auto_now=True)
    item_image_1 = models.ImageField('Первая картинка', upload_to="images", null=True)
    item_image_2 = models.ImageField('Вторая картинка', upload_to="images", null=True)
    item_color = MultiSelectField('Цвета', max_length= 100, choices=colors, null=True)
    item_type = models.ForeignKey('ItemType', on_delete = models.CASCADE, null=True)

    
    class Meta:
        verbose_name= 'Товар'
        verbose_name_plural = 'Товар'

    def __str__(self):
        return '{}'.format(self.item_title)
    
class ItemType(models.Model):
    title = models.CharField('Название товара', max_length=200)

    class Meta:
        verbose_name= 'Тип товара'
        verbose_name_plural = 'Тип товара'

    def __str__(self):
        return '{}'.format(self.title)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Melnikov, 2019-09-09
@maxell

sku = Item.objects.get(item_type ="your type here")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question