[1.0.X] Fixed #10163: add an artificial ordering to querysets used by formsets, thus ensuring that POSTed data "lines up" correctly every time. Thanks to Karen Tracey for pointing in the right direction here.

This is a backport of [10625] from trunk, in a sense. In 1.1 I added a `QuerySet.ordered` property to deal with the logic of determining whether a queryset has ordering, but we can't add new features on a bugfix branch. So here in 1.0-land, the logic has to live in the formset. This smells, but it's better than having a bug.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10630 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2009-04-23 14:18:11 +00:00
parent 91d063c405
commit 3b3c05df72
1 changed files with 18 additions and 0 deletions

View File

@ -388,6 +388,24 @@ class BaseModelFormSet(BaseFormSet):
qs = self.queryset
else:
qs = self.model._default_manager.get_query_set()
# 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.
#
# This logic is in the wrong place here on the 1.0.X branch.
# In the 1.1 series this logic exists as the QuerySet.ordered
# property, but since that's new in 1.1 here in 1.0 we just
# have to deal with this slightly smelly code here.
if qs.query.extra_order_by or qs.query.order_by:
ordered = True
elif qs.query.default_ordering and qs.query.model._meta.ordering:
ordered = True
else:
ordered = False
if not ordered:
qs = qs.order_by(qs.model._meta.pk.name)
if self.max_num > 0:
self._queryset = qs[:self.max_num]
else: