I have a model that looks like this:
class ParentObject(models.Model):
...
services = models.ManyToManyField(Service)
class ChildObject(models.Model):
parent = models.ForeignKey(ParentObject)
services = models.ManyToManyField(Service)
class Service(models.Model):
name开发者_JAVA技巧 = ...
description = ...
So, in summary, an object has a list of Services attached and a child objects has to have other Services attached.
In size, each Parent has a list of 50-60 Services and each Child (5 per Parent) has a list of 30-40 Services. In django-admin (using grappelli btw) I have set Child to inline.
The problem is that the page in admin loads very hard (3-5 seconds) because Django Admin executes about 1200 queries (to get the Service each time - sometimes multiple times) to show me the information to edit.
Do you know any tips/tricks to optimize this?
Thank you in advance.
I haven't been playing with grappelli but in standard django-admin I would consider usage of: ModelAdmin.raw_id_fields. The limitation is that you don't select services using name but using pk.
By default, Django's admin uses a select-box interface () for fields that are ForeignKey. Sometimes you don't want to incur the overhead of having to select all the related instances to display in the drop-down.
raw_id_fields is a list of fields you would like to change into an Input widget for either a ForeignKey or ManyToManyField:
class ArticleAdmin(admin.ModelAdmin):
raw_id_fields = ("newspaper",)
Harder approach would be to override admin manager for Service and add request level cache. Remember to make this manager available in case of elated objects access.
You need to override the admin queryset method and add a select_related in it, put this in your admin.py file
class ServiceAdmin(admin.ModelAdmin):
...
def queryset(self, request):
qs = super(ServiceAdmin, self).queryset(request)
return qs.select_related()
admin.site.register(Service, ServiceAdmin)
This alone should decrease your queries a little bit, but the main problem is that the Django select_related
doesn't follow ManyToMany automatically. You need to do some extra workaround for that. I would use the prefill_entry_list
from the FeinCMS application to do that.
精彩评论