#26800: Model FormSet & Prefetch ordered
-------------------------------------+-------------------------------------
Reporter: amille44420 | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
Keywords: formset preferch | Triage Stage:
order_by ordered model | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by amille44420):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> I happened to meet some unexpected behavior while mixing together the
> Model FormSet with a Queryset on which I had prefetch.
>
> My setup is the following one ;
>
> {{{
> #!python
> prefetch = Prefetch('childs', Child.objects.order_by('order')),
> queryset = Parent.objects.prefetch_related(prefetch)
> parent = queryset.get(pk=42)
> formset = ChildFormSet(queryset=parent.childs.all())
> }}}
>
> Everything works without warning or error, however the formset does not
> respect the queryset I provide. I tried to understand a bit more about
> the reasons behind it and I finally see the folloxing lines in
> BaseModelFormSet.get_queryset() ;
>
> {{{
> "!python
> # If the queryset isn't already ordered we need to add an
> # artificial ordering here to make sure that all formsets
> # constructed from this queryset have the same form order.
> if not qs.ordered:
> qs = qs.order_by(self.model._meta.pk.name)
> }}}
>
> So I went to read my queryset object and I saw that the property ordered
> is to False while the results are correctly ordered (the property
> _prefetch_done is True on my queryset object). So my thinking is that the
> queryset resulting of parent.object.all() is my prefetch (as expected)
> but not ordered as my FormSet expects. So it does order it by the primary
> key for the model and breaks my prefetch.
>
> I checked twice on the documentation and source code but I didn't see any
> warning about putting together prefetch & formset, especially on ordered
> queryset.
New description:
I happened to meet some unexpected behavior while mixing together the
Model FormSet with a Queryset on which I had prefetch.
My setup is the following one ;
{{{
#!python
prefetch = Prefetch('childs', Child.objects.order_by('order')),
queryset = Parent.objects.prefetch_related(prefetch)
parent = queryset.get(pk=42)
formset = ChildFormSet(queryset=parent.childs.all())
}}}
Everything works without warning or error, however the formset does not
respect the queryset I provide. I tried to understand a bit more about the
reasons behind it and I finally see the folloxing lines in
BaseModelFormSet.get_queryset() ;
{{{
#!python
# If the queryset isn't already ordered we need to add an
# artificial ordering here to make sure that all formsets
# constructed from this queryset have the same form order.
if not qs.ordered:
qs = qs.order_by(self.model._meta.pk.name)
}}}
So I went to read my queryset object and I saw that the property ordered
is to False while the results are correctly ordered (the property
_prefetch_done is True on my queryset object). So my thinking is that the
queryset resulting of parent.object.all() is my prefetch (as expected) but
not ordered as my FormSet expects. So it does order it by the primary key
for the model and breaks my prefetch.
I checked twice on the documentation and source code but I didn't see any
warning about putting together prefetch & formset, especially on ordered
queryset.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26800#comment:1>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/069.a0552350cefe2de72b4260faf20f41cc%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.