diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2009-04-23 14:18:11 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2009-04-23 14:18:11 +0000 |
| commit | 3b3c05df72af774edd55d17f575db88786e3973e (patch) | |
| tree | 1792f35a59e0fb4694304b79056e6720ee55948a /django/forms | |
| parent | 91d063c405a8d05a366515d882bd462bf7c00107 (diff) | |
[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
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/models.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/django/forms/models.py b/django/forms/models.py index aa5f6ad795..3d9a795049 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -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: |
