Browse files

[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: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 91d063c commit 3b3c05df72af774edd55d17f575db88786e3973e @jacobian jacobian committed Apr 23, 2009
Showing with 18 additions and 0 deletions.
  1. +18 −0 django/forms/
@@ -388,6 +388,24 @@ def get_queryset(self):
qs = self.queryset
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(
if self.max_num > 0:
self._queryset = qs[:self.max_num]

0 comments on commit 3b3c05d

Please sign in to comment.