Victor
unread,Nov 7, 2013, 4:04:33 AM11/7/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
I made an effort to simplify my app and translate it into English.
Here it is
=================
models.py
.................................
class Items(models.Model):
code = models.CharField(primary_key=True,max_length=15,db_column='code')
description = models.CharField(max_length=255, db_column='Description', db_index=True)
category = models.IntegerField(choices=categoria, db_column='Category',default=2)
total_quantity_in_store=models.IntegerField(db_column='total_quantity_in_store', default=0)
def __unicode__(self):
return self.description
class Meta:
db_table = u'Items'
class Order(models.Model):
id_order = models.IntegerField(primary_key=True,db_column='id_order')
patient = models.ForeignKey(Patients, db_column='patient')
def __unicode__(self):
return u"Ord.%s per %s" % (self.id_order, self.paziente)
class Meta:
db_table = u'Order'
post_save.connect(TotalInStore, sender=Order)
class OrderDetail(models.Model):
id_order = models.ForeignKey(Order,db_column='id_order')
item_code = models.ForeignKey(Items,verbose_name='Items')
quantity = models.IntegerField(db_column='quantity',blank=True,default=0)
class Meta:
db_table = u'OrderDetail'
==================
admin.py
......................
class OrderDetailInline(admin.TabularInline):
model=OrderDetail
raw_id_fields = ['item_code',]
fields=('item_code', 'quantity',)
class OrderOption(admin.ModelAdmin):
readonly_fields = ['order_id', 'patient']
list_display = ( 'patient','order_id')
fields=( 'order_id', 'patient')
inlines=[OrderDetailInline,]
admin.site.register(Order,OrderOption)
....................
=========================
signals.py
def ExecuteQuery(query):
from django.db import connection
cursor=None
cursor= connection.cursor()
cursor.execute(query, [])
return cursor.fetchall()
def TotalInStore(sender,**kwargs):
....................
ItemsInOrder = """SELECT item_code_id as code,SUM(quantity) as total
FROM OrderDetail
GROUP BY item_code_id
ORDER BY item_code_id"""
SUMS = ExecuteQuery(ItemsInOrder)
if SUMS:
[Items.objects.filter(pk=t[0]).update(total_quantity_in_store=int(t[1])) for t in SUMS]
.............
return
To put it in a nutshell my Django 1.5.5 app 1):records the quantities of medical items given to patients of a medical center by means of an inline view that connects 'OrderDetail' to 'Order', 2): then input in the field total_quantity_in_store of the model 'Items' the sum of the field quantity ('OrderDetail') aggregated by item code via a post_save signal calling the TotalInStore function.
It works happily BUT .... unfortunately with the increasing numbers of record in 'OrderDetail' it is (and will be obviously) becoming slower and slower. This is due to the fact that the post_save signal - aggregating the quantities - by code is called for EACH OrderDetail record I have input in the inline view while it will be more logical to execute it ***after*** all records of the inline view have been saved.
Please help me modify my app so that the TotalInStore function is called only once after the inline view Order-OrderDetail has been saved.
Ciao
Vittorio