S
S
sportik1742020-12-19 11:59:55
Django
sportik174, 2020-12-19 11:59:55

How to get collections that have products with a specific room value?

There are 3 models:

ProductCollections = collections that include products

class ProductCollections(models.Model):
    title = models.CharField(max_length=255, verbose_name='Название')
    slug = models.SlugField(unique=True)
    img = models.ImageField(verbose_name='Фото')
    price = models.PositiveIntegerField(default=0, verbose_name='Цена от')

    def __str__(self):
        return self.title
        
    class Meta:
        verbose_name = 'Коллекция'
        verbose_name_plural = 'Коллекции'


Product = goods

class Product(models.Model):
    # collection = models.ForeignKey("Collections", verbose_name="Коллекция", on_delete=models.CASCADE)
    
    title = models.CharField(max_length=255, verbose_name="Название")
    slug = models.SlugField(unique=True)
    price = models.PositiveIntegerField( default=0, verbose_name="Цена")
    img = models.ImageField(verbose_name='Фото')
    # Many to many
    collections = models.ManyToManyField('ProductCollections', verbose_name='Колекция')
    premises = models.ManyToManyField('Premises', verbose_name='Помещение')

    def __str__(self):
        return self.title

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


Premises = premises

class Premises(models.Model):
    title = models.CharField(max_length=255, verbose_name='Название')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '--- Помещение'
        verbose_name_plural = '--- Помещения'


How to get collections that have products with a specific room value?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VkolV, 2020-12-30
@VkolV

First, select the goods corresponding to this room, and determine which collections they belong to by the goods.
ManyToManyField - one product in different rooms and different collections (somewhat redundant in my opinion)?
174? Chelyabinsk? Countryman! :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question