Hello again,
I would like to clarify that the big number of db requests is not caused
by fetching all the objects for the related model to populace select
boxes for the related fields in the line, as it is often the case. To
avoid that, I have included the related fields in raw_id_fields. I have
also used grappelli's convenient autocomplete lookup feature, but that
does not add extra queries. So, the inline class actually looks like this:
class ExampleBInline(admin.StackedInline):
model = ExampleB
extra = 0
raw_id_fields = ['cexample', 'dexample', 'eexample']
autocomplete_lookup_fields = {
'fk': ['cexample', 'dexample', 'example'],
}
The only optimization that works is adding the related fields in
readonly_fields in combination with a queryset changing method with
select_related(), like this:
class ExampleBInline(admin.StackedInline):
model = ExampleB
extra = 0
raw_id_fields = ['cexample', 'dexample', 'eexample']
autocomplete_lookup_fields = {
'fk': ['cexample', 'dexample', 'eexample'],
}
readonly_fields = ['cexample', 'dexample', 'eexample']
def get_queryset(self, request):
qs = super(ExampleBInline, self).get_queryset(request)
return qs.select_related('cexample', 'dexample', 'eexample')
However, I am not satisfied with that solution because I do want to be
able to edit the related fields inline.