1

I have two models:

models.py

class City(models.Model):
    title = models.CharField(max_length=255)
    show = models.BooleanField(default=True)

class Company(models.Model)
    title = models.CharField(max_length=255)
    cities = models.ManyToManyField(City, null=True, blank = True)

admin.py

class CompanyAdmin(admin.ModelAdmin):
    search_fields = ('title',)
    filter_horizontal = ('cities',)

It is about 23000 cities in the database.

When i edit the Company detail in Admin it loads forever!!! Just accessing admin/myapp/company/12/ takes 2-3 minutes - this is horrible.

How can i speed up things and cache City model queryset?

Feanor
  • 3,568
  • 5
  • 29
  • 49

2 Answers2

1

You can use raw_id_fields

class CompanyAdmin(admin.ModelAdmin):
    raw_id_fields = ("cities",)
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
1

23000 is actually "nothing" for a database.

First, you need to identify the "culprit" of a slowdown, which query is slow. This is where django-debug-toolbar would help a lot. Then, you can run the queries manually with EXPLAIN and see what can be improved - for example, adding indexes could help.

Also, consider using django-cache-machine or johnny-cache packages.

Also see:

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195