C
C
Canister Kanistrov2021-11-30 16:29:58
Python
Canister Kanistrov, 2021-11-30 16:29:58

How to borrow a value from one model to another?

I need to borrow the value of the storage_number string from this model

class Storage(models.Model):
    
    STORAGE_CATEGORY_CHOICES = (
     ('Винтовой компрессор', 'Винтовой компрессор'),
     ('Осушитель сжатого воздуха', 'Осушитель сжатого воздуха'),
    )
    
    STORAGE_IP_CHOICES = (
     ('23', '23'),
     ('54', '54'),
    )
    
    STORAGE_PRESSURE_CHOICES = (
     ('8', '8'),
     ('10', '10'),
     ('13', '13'),
    )
    
    STORAGE_DRIVE_UNIT_CHOICES = (
     ('Ременной', 'Ременной'),
     ('Прямой', 'Прямой'),
    )

    storage_name = models.CharField('Оборудование', max_length=30)
    storage_bar = models.CharField('Давление', max_length=12, choices=STORAGE_PRESSURE_CHOICES)
    storage_ip = models.CharField('IP', max_length=12, choices=STORAGE_IP_CHOICES)
    storage_category = models.CharField('Категория', max_length=50, choices=STORAGE_CATEGORY_CHOICES)
    storage_drive_unit = models.CharField('Привод', max_length=12, choices=STORAGE_DRIVE_UNIT_CHOICES)
    storage_performance = models.CharField('Производительность', max_length=12)
    storage_power = models.CharField('Мощность', max_length=12)
    storage_outlet_pipe_diameter = models.CharField('Диаметр выходной трубы', max_length=12)
    storage_dimensions = models.CharField('Габариты', max_length=15)
    storage_mass = models.CharField('Масса', max_length=12)
    storage_number = models.IntegerField('На складе')
    storage_reserv_number = models.IntegerField('Из них в резерве')
    storage_image = models.CharField('Изображение', max_length=255)

    def __str__(self):
        return str(self.storage_category + " " + self.storage_name + " (" + self.storage_bar + " бар) (IP " + self.storage_ip + ")")

    class Meta:
        unique_together = ('storage_name', 'storage_bar', 'storage_ip')
        verbose_name = 'Склад'
        verbose_name_plural = 'Склад'

Here in equipment_number
class Equipment(models.Model):
    equipment_client = models.ForeignKey(Clients, verbose_name='Клиент', on_delete=models.CASCADE, max_length=50)
    equipment_equipment = models.ForeignKey(Storage, verbose_name='Оборудование', on_delete=models.CASCADE, max_length=50)
    equipment_dealer = models.ForeignKey(User, verbose_name='Дилер', on_delete=models.CASCADE, max_length=50)
    equipment_number = models.IntegerField('Количество')
    equipment_createdate = models.DateField(verbose_name='Дата бронирования', default=(timezone.localtime(timezone.now()).strftime('%Y-%m-%d')), max_length=50)
    equipment_deletedate = models.DateField(verbose_name='Дата снятия брони', default=(timezone.localtime(timezone.now()+timedelta(days=7)).strftime('%Y-%m-%d')), max_length=50)

    def __str__(self):
        return self.equipment_dealer.username

    class Meta:
        unique_together = ('equipment_client', 'equipment_equipment')
        verbose_name = 'Оборудование'
        verbose_name_plural = 'Оборудование

How to do it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-11-30
@fox_12

1. Do a migration with the creation of storage_number (or whatever you call it ...) from the Equipment model
2. Do a migration with the transfer of field data from the Storage model to Equipment
3. Do a migration with the removal of the storage_number field from the Storage model
All ...
If you migrate it is not necessary - then in each object of the Equipment model it is already in the .equipment_equipment field. storage_number
PS: Why duplicate the model name in the field name?
Is the name of an object like equipment.equipment_client more readable than equipment.client ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question