P
P
paevlk20072014-08-17 11:21:48
Django
paevlk2007, 2014-08-17 11:21:48

Django - how to write off quantity from one model to another?

Hello!
There is a model:

class Material(models.Model):
    name = models.CharField("Материал:", max_length=100)
    coming = models.IntegerField(u"Приход Кол-во")
    balance = models.IntegerField(u"Остаток Кол-во")

class Plot(models.Model):
    plot = models.CharField("Участок:", max_length=100)
    material = models.ForeignKey(Material, verbose_name=u"Материал")
    expenditure = models.IntegerField(u"Израсходование Кол-во Материала")

    def amount(self):
        try:
            calculation = ((self.material.coming) - (self.expenditure))
            return "%d" % calculation
        except:
            return False

@receiver(pre_save, sender=Plot)
def total(sender, instance, **kwargs):
    instance.material.balance = instance.calculation()

Task, there is a Material model, with the quantity of the arrival of the Material, let's say 10 pcs. There is a Plot model, where the material consumption is indicated, it is necessary to write off exactly the material that was selected in the Plot model, if the material is written off more than it is in the form, this is indicated in the form. The balance of the material is indicated in the Material model in the balance field.
Maybe in the form to produce a reduction in quantity?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
paevlk2007, 2014-08-19
@paevlk2007

ay

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question