S
S
sportik1742020-12-18 18:46:18
Django
sportik174, 2020-12-18 18:46:18

How to get ForeignKey and Many To Many model objects?

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='Цена от')
    fabrika = models.ForeignKey('Fabriks', verbose_name='Производитель', on_delete=models.CASCADE)
    category = models.ForeignKey('Category', verbose_name='Категория', on_delete=models.CASCADE)

    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='Фото')
    #One to Many
    price_type = models.ForeignKey('PriceType', verbose_name='Цена за', on_delete=models.CASCADE)
    widht = models.ForeignKey('Width', verbose_name='Ширина, см', on_delete=models.CASCADE)
    height = models.ForeignKey('Height', verbose_name='Высота, см', on_delete=models.CASCADE)
    square = models.ForeignKey('Square', verbose_name='Площадь, м2', on_delete=models.CASCADE)
    number_of_square_meters = models.ForeignKey('NumberOfSquareMeters', verbose_name='Количество плитки в коробке, кв.м.', on_delete=models.CASCADE)
    quantity_pcs = models.ForeignKey('QuantityPcs', verbose_name='Кол-во плитки в коробке, шт.', on_delete=models.CASCADE)
    weight_pcs = models.ForeignKey('WeightPcs', verbose_name='Вес 1 шт., кг', on_delete=models.CASCADE)
    weight_sq = models.ForeignKey('WeightSq', verbose_name='Вес 1 м.кв.', on_delete=models.CASCADE)
    appointment = models.ForeignKey('Appointment', verbose_name='Назначение', on_delete=models.CASCADE)
    # Many to many
    collections = models.ManyToManyField('ProductCollections', verbose_name='Колекция')
    color = models.ManyToManyField('Color', verbose_name='Цвет')
    picture = models.ManyToManyField('Picture', verbose_name='Рисунок')
    form = models.ManyToManyField('Form', verbose_name='Форма')
    surface = models.ManyToManyField('Surface', verbose_name='Поверхность')
    premises = models.ManyToManyField('Premises', verbose_name='Помещение')
    # Boolean
    rectificate = models.BooleanField(verbose_name='Ректификат?', default=False)
    stock = models.BooleanField(verbose_name='Акция?', default=False)
    sale = models.BooleanField(verbose_name='Распродажа?', default=False)

    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)
D
Dr. Bacon, 2020-12-18
@bacon

And what is not immediately "Complicated" to put, relationship is the basics and entry level in django, so read the docks https://docs.djangoproject.com/en/3.1/topics/db/qu... and https:// docs.djangoproject.com/en/3.1/topics/db/qu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question