class Sklad models Model handbookmodel models ForeignKey HandBookModel

 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
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()
views:
def listsklad(request):
# listsklad = Sklad.objects.all()
listsklad = Sklad.objects.extra(
select={
'sklad_sum': 'SELECT SUM(sklad_compbill.quantity_numberbill) FROM sklad_sklad, sklad_compbill WHERE sklad_compbill.id = sklad_sklad.id'
},
)
template:
{% extends "base.html" %}
{% block content %}
{% if object_list %}
<h2>Последние поступуления на складе</h2>
<table>
{% for sklad in object_list %}
<tr>
<td>{{ sklad.listsklad }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>На складе ничего нет.</p>
{% endif %}
{% endblock %}
return render_to_response('sklad_list.html',{'object_list':listsklad})