#32244: ORM inefficiency: ModelFormSet executes a single-object SELECT query per
formset instance when saving/validating
-------------------------------------+-------------------------------------
Reporter: Lushen Wu | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: formsets | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by powderflask):
Thanks Carlton for your serious consideration of this issue and deep
explanations - it's very kind of you to spend time on this.
I hit this issue for a 3rd time, so had another chance to think it
through, but still don't see where I've misunderstood anything
fundamental...
But the lookup is **not** redundant… — it's a ModelChoiceField: it has
a QuerySet of possible valid values, and in validation we need to check
that the submitted value is in the QuerySet.
You are right in general - a ModelChoiceField needs a QuerySet to
validate. What I think I'm arguing is that hidden PK field should not be
a ModelChoiceField at all - the overhead is not needed.
Here's why I think the Queryset / ModelChoiceField is redundant and could
be replaced by a simpler type of field:
1. this is a hidden field precisely because the PK field can't be edited.
In fact, it should not be valid for that hidden field to have any value
other than None (for new forms) or the PK of the form instance. Any
other value should raise a validation error.
2. so we don't need a QuerySet to do the validation.
- If the hidden PK field is empty, great, it's a new instance,
nothing to validate;
- otherwise, it's value MUST match the form's instance - if it
doesn't, something has gone horribly wrong in any case.
Perfectly possible I've just missed some crucial use case or fact about
how model formsets are validated. But I've been through this 3 times now
for 3 different use cases, and come to the same conclusion each time -
valid values for the PK are already known at validation time, so the DB
access to fetch the Queryset is just pure overhead (for reference, I have
forms where >500 entries are being submitted, so the DB lag is non-
trivial).
thanks again - sorry to be a dog with a bone :-P
--
Ticket URL: <https://code.djangoproject.com/ticket/32244#comment:15>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070183ab002259-c917064c-1f54-492a-89ee-16d2cf1d97ba-000000%40eu-central-1.amazonses.com.