M
M
Maxim Zubenko2018-09-03 15:38:12
Django
Maxim Zubenko, 2018-09-03 15:38:12

Django: How to make a model with unique dependencies?

Such a situation. There are several simple models:

class TypeProfile(models.Model):
    """ Модель профиля """
 
    title = models.CharField(max_length=30, verbose_name='Название профиля', db_index=True)
    description = models.CharField(max_length=300, verbose_name='Описание')
 
class TypeFacade(models.Model):
    """ Вид фасада """
    
    title = models.CharField(max_length=30, verbose_name='Вид фасада', db_index=True)
    description = models.CharField(max_length=300, verbose_name='Описание')

And there is a pricing model.
class Price(models.Model):
    """ Ценник """
    name_profile = models.ForeignKey(TypeProfile, on_delete=models.PROTECT, verbose_name='Модель профиля')
    type_facade = models.ForeignKey(TypeFacade, on_delete=models.PROTECT, verbose_name='Вид фасада')
    stain_price = models.DecimalField(max_digits=8, decimal_places=2, blank=True, verbose_name='Цена морилка')
    enamel_price = models.DecimalField(max_digits=8, decimal_places=2, blank=True, verbose_name='Цена эмаль')
    description = models.CharField(max_length=300, verbose_name='Описание')

How to make the last (price tag) so that when a new record is formed, a check is made, or maybe there already was such a combination of name_profile + type_facade?
For example.
There are profiles: 1,2,3,4
There are facades: a,b,c,d
When forming a price tag, there should be only one entry 1a, 1b, 1c, 1d, etc. those. 2a, 2b, 2c, 2d ...
And if once again, when forming the price tag, a combination, for example, 2b, is obtained, it cursed and said that it already exists.
I hope I explained clearly. Sincerely.
ps in the example, there are 2 dependencies, but in general there can be 3 or more of them.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question