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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ED9F8BF6-24E4-49F2-9721-5FAECDFBABA6%40gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to