class HandBookModel models Model handbookmanufacturer models ForeignKe

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class HandBookModel(models.Model):
handbookmanufacturer = models.ForeignKey(HandBookManufacturer, verbose_name=u"Производитель")
article = models.CharField(u'Артикул',maxlength=15, unique=True)
all_quantity = models.IntegerField(u'Количество', null=True, editable=False)
def __unicode__(self):
return '%s, %s' % (self.handbookmanufacturer.manufacturer, self.article)
class Admin:
pass
class Sklad(models.Model):
handbookmodel = models.ForeignKey(HandBookModel, verbose_name=u"Артикул")
compbill = models.ForeignKey('CompBill', unique=True, verbose_name=u"По накладной")
# reserving = models.ForeignKey('Reserving')
def save(self):
super(Sklad, self).save()
if self.compbill.type_effects == 'A':
if self.handbookmodel.all_quantity is None:
self.handbookmodel.all_quantity = 0
self.handbookmodel.all_quantity += self.compbill.quantity_numberbill
else:
# self.handbookmodel.all_quantity = sum(map(lambda x: x.quantity_numberbill,CompBill.objects.all()))
self.handbookmodel.all_quantity += self.compbill.quantity_numberbill
else:
# a = self.handbookmodel.all_quantity -= self.compbill.quantity_numberbill
# if a < 0:
# вывод ошибки
self.handbookmodel.all_quantity -= self.compbill.quantity_numberbill
return super(HandBookModel, self.handbookmodel).save()
def __unicode__(self):
return u'Модель %s, c количеством %d' % (self.handbookmodel.article, self.compbill.quantity_numberbill)
class Admin:
pass
class CompBill(models.Model):
quantity_numberbill = models.IntegerField(u'Количество')
bill = models.ForeignKey('Bill', unique=True)
type_effects = models.CharField(u'Тип записи',max_length=1, choices=TYPE_CHOICES)
def __unicode__(self):
return '%d' % (self.bill.number_bill)
class Admin:
pass
class Bill(models.Model):
number_bill = models.AutoField(u'Номер накладной','ID', primary_key=True)
date_number_bill = models.DateField(u'Дата накладной')
user = models.ForeignKey(User)