Multiple Queries For Inline instances Django admin












0















I have created a custom Admin for one of my model. And added a Relational table as one of its inline.



1). Now the thing is that Inline has over a 100 rows. And the inline further has a foreign key object to some other model.



2).Whenever i load the model form it takes a whole lot of time to Load.



3).I debugged and checked the code and number of queries. No query was duplicated and the count was nominal.



4).I have overridden the query set for the inline instance to prefetch its foreign key instance.



5).I went on and raised a random validation error in the formset's clean.



6).What i found out that when the error was raised it had a different query for every inline instance. Say if it has a 100 rows then the query was duplicated 100 times for that inline.



7).Is there any way to prefetch or optimize this scenario as its causing way too lag in my application



Here's the Code:



#model
class MyModel(models.Model):

user = models.ForeignKey(User, on_delete=models.CASCADE)
phone_number = models.CharField(max_length=10)
entity1 = models.ForeignKey("entities.entity1", null=True, blank=True, on_delete=models.CASCADE)
entity2 = models.ForeignKey("entities.entity2", null=True, blank=True, on_delete=models.CASCADE)
is_disabled = models.BooleanField(default=False)
name = models.CharField(max_length=24, blank=True, null=True)

class Meta:
db_table = 'my_model'

def __str__(self):
return "{}:{}".format(self.phone_number, self.entity2)


#admin
class Entity2Admin(admin.GeoModelAdmin, VersionAdmin):
list_filter = ('data_status')
readonly_fields = ('source', 'batch','is_live', )
exclude = ('search_key', 'live_at', 'qc_approved_at')

def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)

for obj in formset.deleted_objects:
obj.delete()

for instance in instances:
if isinstance(instance, MyModel):
if (not instance.created_by):
instance.created_by = request.user
if (not instance.id):
instance.some_field = some_value
instance.save()
formset.save_m2m()

inlines = [MyModelInline]

extra_js = ['js/admin/GoogleMap.js','https://maps.googleapis.com/maps/api/js?key=AIzaSyA-5gVhxxxxxxxxxxxxxxxxxxxxxxx&callback=initGoogleMap']


#MyModelInline
class MyModelInline(admin.TabularInline):
model = MyModel
extra = 0
can_delete = True
show_change_link = False
formset = MyModelFormSet
readonly_fields = ['user']
verbose_name_plural = "MyModels"
fields = ['phone_number', 'name', 'user', 'entity2']

def get_queryset(self, request):
return super(MyModelInline, self).get_queryset(request).select_related('entity2', 'user')

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "entity2":
object_id = request.resolver_match.kwargs.get('object_id')
kwargs["queryset"] = Entity2.objects.filter(field=value)
return super().formfield_for_foreignkey(db_field, request, **kwargs)


Please help










share|improve this question

























  • We can't help without seeing some code.

    – Daniel Roseman
    Nov 20 '18 at 12:09











  • Add your model, admin and if you're using a custom form, add that too please.

    – schillingt
    Nov 20 '18 at 13:28











  • @DanielRoseman,,,,,,i have added the code...can u please have a look

    – ronit
    Nov 21 '18 at 6:03
















0















I have created a custom Admin for one of my model. And added a Relational table as one of its inline.



1). Now the thing is that Inline has over a 100 rows. And the inline further has a foreign key object to some other model.



2).Whenever i load the model form it takes a whole lot of time to Load.



3).I debugged and checked the code and number of queries. No query was duplicated and the count was nominal.



4).I have overridden the query set for the inline instance to prefetch its foreign key instance.



5).I went on and raised a random validation error in the formset's clean.



6).What i found out that when the error was raised it had a different query for every inline instance. Say if it has a 100 rows then the query was duplicated 100 times for that inline.



7).Is there any way to prefetch or optimize this scenario as its causing way too lag in my application



Here's the Code:



#model
class MyModel(models.Model):

user = models.ForeignKey(User, on_delete=models.CASCADE)
phone_number = models.CharField(max_length=10)
entity1 = models.ForeignKey("entities.entity1", null=True, blank=True, on_delete=models.CASCADE)
entity2 = models.ForeignKey("entities.entity2", null=True, blank=True, on_delete=models.CASCADE)
is_disabled = models.BooleanField(default=False)
name = models.CharField(max_length=24, blank=True, null=True)

class Meta:
db_table = 'my_model'

def __str__(self):
return "{}:{}".format(self.phone_number, self.entity2)


#admin
class Entity2Admin(admin.GeoModelAdmin, VersionAdmin):
list_filter = ('data_status')
readonly_fields = ('source', 'batch','is_live', )
exclude = ('search_key', 'live_at', 'qc_approved_at')

def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)

for obj in formset.deleted_objects:
obj.delete()

for instance in instances:
if isinstance(instance, MyModel):
if (not instance.created_by):
instance.created_by = request.user
if (not instance.id):
instance.some_field = some_value
instance.save()
formset.save_m2m()

inlines = [MyModelInline]

extra_js = ['js/admin/GoogleMap.js','https://maps.googleapis.com/maps/api/js?key=AIzaSyA-5gVhxxxxxxxxxxxxxxxxxxxxxxx&callback=initGoogleMap']


#MyModelInline
class MyModelInline(admin.TabularInline):
model = MyModel
extra = 0
can_delete = True
show_change_link = False
formset = MyModelFormSet
readonly_fields = ['user']
verbose_name_plural = "MyModels"
fields = ['phone_number', 'name', 'user', 'entity2']

def get_queryset(self, request):
return super(MyModelInline, self).get_queryset(request).select_related('entity2', 'user')

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "entity2":
object_id = request.resolver_match.kwargs.get('object_id')
kwargs["queryset"] = Entity2.objects.filter(field=value)
return super().formfield_for_foreignkey(db_field, request, **kwargs)


Please help










share|improve this question

























  • We can't help without seeing some code.

    – Daniel Roseman
    Nov 20 '18 at 12:09











  • Add your model, admin and if you're using a custom form, add that too please.

    – schillingt
    Nov 20 '18 at 13:28











  • @DanielRoseman,,,,,,i have added the code...can u please have a look

    – ronit
    Nov 21 '18 at 6:03














0












0








0








I have created a custom Admin for one of my model. And added a Relational table as one of its inline.



1). Now the thing is that Inline has over a 100 rows. And the inline further has a foreign key object to some other model.



2).Whenever i load the model form it takes a whole lot of time to Load.



3).I debugged and checked the code and number of queries. No query was duplicated and the count was nominal.



4).I have overridden the query set for the inline instance to prefetch its foreign key instance.



5).I went on and raised a random validation error in the formset's clean.



6).What i found out that when the error was raised it had a different query for every inline instance. Say if it has a 100 rows then the query was duplicated 100 times for that inline.



7).Is there any way to prefetch or optimize this scenario as its causing way too lag in my application



Here's the Code:



#model
class MyModel(models.Model):

user = models.ForeignKey(User, on_delete=models.CASCADE)
phone_number = models.CharField(max_length=10)
entity1 = models.ForeignKey("entities.entity1", null=True, blank=True, on_delete=models.CASCADE)
entity2 = models.ForeignKey("entities.entity2", null=True, blank=True, on_delete=models.CASCADE)
is_disabled = models.BooleanField(default=False)
name = models.CharField(max_length=24, blank=True, null=True)

class Meta:
db_table = 'my_model'

def __str__(self):
return "{}:{}".format(self.phone_number, self.entity2)


#admin
class Entity2Admin(admin.GeoModelAdmin, VersionAdmin):
list_filter = ('data_status')
readonly_fields = ('source', 'batch','is_live', )
exclude = ('search_key', 'live_at', 'qc_approved_at')

def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)

for obj in formset.deleted_objects:
obj.delete()

for instance in instances:
if isinstance(instance, MyModel):
if (not instance.created_by):
instance.created_by = request.user
if (not instance.id):
instance.some_field = some_value
instance.save()
formset.save_m2m()

inlines = [MyModelInline]

extra_js = ['js/admin/GoogleMap.js','https://maps.googleapis.com/maps/api/js?key=AIzaSyA-5gVhxxxxxxxxxxxxxxxxxxxxxxx&callback=initGoogleMap']


#MyModelInline
class MyModelInline(admin.TabularInline):
model = MyModel
extra = 0
can_delete = True
show_change_link = False
formset = MyModelFormSet
readonly_fields = ['user']
verbose_name_plural = "MyModels"
fields = ['phone_number', 'name', 'user', 'entity2']

def get_queryset(self, request):
return super(MyModelInline, self).get_queryset(request).select_related('entity2', 'user')

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "entity2":
object_id = request.resolver_match.kwargs.get('object_id')
kwargs["queryset"] = Entity2.objects.filter(field=value)
return super().formfield_for_foreignkey(db_field, request, **kwargs)


Please help










share|improve this question
















I have created a custom Admin for one of my model. And added a Relational table as one of its inline.



1). Now the thing is that Inline has over a 100 rows. And the inline further has a foreign key object to some other model.



2).Whenever i load the model form it takes a whole lot of time to Load.



3).I debugged and checked the code and number of queries. No query was duplicated and the count was nominal.



4).I have overridden the query set for the inline instance to prefetch its foreign key instance.



5).I went on and raised a random validation error in the formset's clean.



6).What i found out that when the error was raised it had a different query for every inline instance. Say if it has a 100 rows then the query was duplicated 100 times for that inline.



7).Is there any way to prefetch or optimize this scenario as its causing way too lag in my application



Here's the Code:



#model
class MyModel(models.Model):

user = models.ForeignKey(User, on_delete=models.CASCADE)
phone_number = models.CharField(max_length=10)
entity1 = models.ForeignKey("entities.entity1", null=True, blank=True, on_delete=models.CASCADE)
entity2 = models.ForeignKey("entities.entity2", null=True, blank=True, on_delete=models.CASCADE)
is_disabled = models.BooleanField(default=False)
name = models.CharField(max_length=24, blank=True, null=True)

class Meta:
db_table = 'my_model'

def __str__(self):
return "{}:{}".format(self.phone_number, self.entity2)


#admin
class Entity2Admin(admin.GeoModelAdmin, VersionAdmin):
list_filter = ('data_status')
readonly_fields = ('source', 'batch','is_live', )
exclude = ('search_key', 'live_at', 'qc_approved_at')

def save_formset(self, request, form, formset, change):
instances = formset.save(commit=False)

for obj in formset.deleted_objects:
obj.delete()

for instance in instances:
if isinstance(instance, MyModel):
if (not instance.created_by):
instance.created_by = request.user
if (not instance.id):
instance.some_field = some_value
instance.save()
formset.save_m2m()

inlines = [MyModelInline]

extra_js = ['js/admin/GoogleMap.js','https://maps.googleapis.com/maps/api/js?key=AIzaSyA-5gVhxxxxxxxxxxxxxxxxxxxxxxx&callback=initGoogleMap']


#MyModelInline
class MyModelInline(admin.TabularInline):
model = MyModel
extra = 0
can_delete = True
show_change_link = False
formset = MyModelFormSet
readonly_fields = ['user']
verbose_name_plural = "MyModels"
fields = ['phone_number', 'name', 'user', 'entity2']

def get_queryset(self, request):
return super(MyModelInline, self).get_queryset(request).select_related('entity2', 'user')

def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "entity2":
object_id = request.resolver_match.kwargs.get('object_id')
kwargs["queryset"] = Entity2.objects.filter(field=value)
return super().formfield_for_foreignkey(db_field, request, **kwargs)


Please help







django python-3.x django-models django-forms django-admin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 6:46







ronit

















asked Nov 20 '18 at 12:03









ronitronit

1331116




1331116













  • We can't help without seeing some code.

    – Daniel Roseman
    Nov 20 '18 at 12:09











  • Add your model, admin and if you're using a custom form, add that too please.

    – schillingt
    Nov 20 '18 at 13:28











  • @DanielRoseman,,,,,,i have added the code...can u please have a look

    – ronit
    Nov 21 '18 at 6:03



















  • We can't help without seeing some code.

    – Daniel Roseman
    Nov 20 '18 at 12:09











  • Add your model, admin and if you're using a custom form, add that too please.

    – schillingt
    Nov 20 '18 at 13:28











  • @DanielRoseman,,,,,,i have added the code...can u please have a look

    – ronit
    Nov 21 '18 at 6:03

















We can't help without seeing some code.

– Daniel Roseman
Nov 20 '18 at 12:09





We can't help without seeing some code.

– Daniel Roseman
Nov 20 '18 at 12:09













Add your model, admin and if you're using a custom form, add that too please.

– schillingt
Nov 20 '18 at 13:28





Add your model, admin and if you're using a custom form, add that too please.

– schillingt
Nov 20 '18 at 13:28













@DanielRoseman,,,,,,i have added the code...can u please have a look

– ronit
Nov 21 '18 at 6:03





@DanielRoseman,,,,,,i have added the code...can u please have a look

– ronit
Nov 21 '18 at 6:03












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392615%2fmultiple-queries-for-inline-instances-django-admin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392615%2fmultiple-queries-for-inline-instances-django-admin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith