Opened 3 years ago
Closed 3 years ago
#33485 closed Cleanup/optimization (duplicate)
inlineformset_factory generate more db subquerys when the model have fk
Reported by: | Reynaldo Juan Pereira Heredia | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 4.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
in line 859 from django.forms.models the variable qs makes a new query from model._default_manager.get_queryset() insted of take values generated by queryset (in my case my queryset habe select_related and prefetch_related data because I don't want to repeat the subqueries internally) in inlineformset_factory.
Reason why having fk in the model generates many subqueries to collect again the data that already exists in the queryset variable, I have not found a better way to avoid such subqueries, so this seems to me to be an error
if isinstance(pk, (ForeignKey, OneToOneField)): qs = pk.remote_field.model._default_manager.get_queryset() else: qs = self.model._default_manager.get_queryset() qs = qs.using(form.instance._state.db) if form._meta.widgets: widget = form._meta.widgets.get(self._pk_field.name, HiddenInput) else: widget = HiddenInput form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=widget) super().add_fields(form, index) {{{ }}}
Attachments (1)
Change History (2)
by , 3 years ago
Attachment: | django querys.png added |
---|
comment:1 by , 3 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Type: | Uncategorized → Cleanup/optimization |
Duplicate of #18597.
when have fk in model generate more db querys