How to use memcache with the queryset of the popup window of a raw field?

27 views
Skip to first unread message

Victor

unread,
Apr 8, 2016, 5:42:33 AM4/8/16
to django...@googlegroups.com
Under Django 1.9.5 and admin I'm developping an application "myapp" of which below you can see a simplified complete version.

Focusing on the OrderOption ModelAdmin its inline OrderDetailinline has a raw_id_field related to the model Item. Now, when I click on the magnifying lens close to the raw_id_field a popup window - as expected- is fired which allows the user to choose an item from the Item model list.

Now, in the real application the popup window is somewhat of a bottleneck because it requires about 4-6 sec. to be loaded owing to the fact that there are many records in the Item model.
Luckily the Item model records change very slowly over time. In a week, say, no more than five items are either added or modified or seldom deleted.
What I'd like to do is using the memcache backend to save the queryset of the Item model - which will appear in the popup window - on my memcached server and recall this same queryset each time the popup window of the raw_id_field is fired avoiding the execution of the query.

My problem is that I don't know how to do it. Please help!

By the way, using the debug_toolbar I see that the popup window uses the view function django.contrib.admin.options.changelist_view, no arguments, and the corresponding url name is myapp_item_changelist.
The sql query is obviously
SELECT "Items"."code", "Items"."Description", "Items"."Category" FROM "Items" ORDER BY"Items"."code" DESC

Thanks
Vittorio


==============================

models.py
from django.db import models

# Create your models here.
class Item(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=[(1,"Cat_1"),(2,"Cat_2")], db_column='Category',default=1)
def __unicode__(self):
return self.description
class Meta:
db_table = u'Items'

class Patient(models.Model):
name = models.CharField(max_length=255, db_column='Description', db_index=True)
def __unicode__(self):
return self.name
class Meta:
db_table = u'Patients'

class Order(models.Model):
patient = models.ForeignKey(Patient, db_column='patient')
def __unicode__(self):
return u"Ord.%s per %s" % (self.id_order, self.patient)
class Meta:
db_table = u'Order'

class OrderDetail(models.Model):
id_order = models.ForeignKey(Order,db_column='id_order')
item_code = models.ForeignKey(Item,verbose_name='Items')
quantity = models.IntegerField(db_column='quantity',blank=True,default=0)
class Meta:
db_table = u'OrderDetail'
==============================

My admin.py
from django.contrib import admin
from myapp.models import *
# Register your models here.

class PatientOption(admin.ModelAdmin):
list_display = ( 'name',)
fields=( 'name',)

class ItemOption(admin.ModelAdmin):
list_display = ( 'code','description')
fields=('code','description')

class OrderDetailInline(admin.TabularInline):
model=OrderDetail
raw_id_fields = ['item_code',]
fields=('item_code', 'quantity',)

class OrderOption(admin.ModelAdmin):
readonly_fields = ['patient']
list_display = ( 'patient',)
fields=( 'patient',)
inlines=[OrderDetailInline,]

admin.site.register(Patient,PatientOption)
admin.site.register(Item,ItemOption)
admin.site.register(Order,OrderOption)


Reply all
Reply to author
Forward
0 new messages