From 7d11c30994c5f90603264c6e2fcf1d9c79ab8924 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 22 Apr 2011 12:01:33 +0000 Subject: [PATCH] Fixed #6953 -- Correctly sort ManyToMany fields in ModelForms. Thanks, dgouldin, mk and Alex. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16063 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/models.py | 2 +- tests/modeltests/model_forms/models.py | 66 +++++++++++++------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/django/forms/models.py b/django/forms/models.py index 8a48e155df5..9c1287f065c 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -143,7 +143,7 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_c field_list = [] ignored = [] opts = model._meta - for f in opts.fields + opts.many_to_many: + for f in sorted(opts.fields + opts.many_to_many): if not f.editable: continue if fields is not None and not f.name in fields: diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 9a962ad72eb..6cd1a72c8a2 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -371,7 +371,7 @@ familiar with the mechanics. OddForm is now an Article-related thing, because BadForm.Meta overrides CategoryForm.Meta. >>> OddForm.base_fields.keys() -['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories'] +['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status'] >>> class ArticleForm(ModelForm): ... class Meta: @@ -382,7 +382,7 @@ First class with a Meta class wins. >>> class BadForm(ArticleForm, CategoryForm): ... pass >>> OddForm.base_fields.keys() -['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories'] +['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status'] Subclassing without specifying a Meta on the class will use the parent's Meta (or the first parent in the MRO if there are multiple parent classes). @@ -556,17 +556,17 @@ fields with the 'choices' attribute are represented by a ChoiceField. Article: +Categories:
Hold down "Control", or "Command" on a Mac, to select more than one. Status: -Categories:
Hold down "Control", or "Command" on a Mac, to select more than one. You can restrict a form to a subset of the complete list of fields by providing a 'fields' argument. If you try to save a @@ -612,17 +612,17 @@ True
  • Article:
  • +
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • Status:
  • -
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • >>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': unicode(w_royko.pk), 'article': 'Hello.'}, instance=art) >>> f.errors {} @@ -675,17 +675,17 @@ Add some categories and test the many-to-many form output.
  • Article:
  • +
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • Status:
  • -
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • Initial values can be provided for model forms >>> f = TestArticleForm(auto_id=False, initial={'headline': 'Your headline here', 'categories': [str(c1.id), str(c2.id)]}) @@ -699,17 +699,17 @@ Initial values can be provided for model forms
  • Article:
  • +
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • Status:
  • -
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • >>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', ... 'writer': unicode(w_royko.pk), 'article': u'Hello.', 'categories': [unicode(c1.id), unicode(c2.id)]}, instance=new_art) @@ -818,17 +818,17 @@ the data in the database when the form is instantiated.
  • Article:
  • +
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • Status:
  • -
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • >>> c4 = Category.objects.create(name='Fourth', url='4th') >>> c4 @@ -845,18 +845,18 @@ the data in the database when the form is instantiated.
  • Article:
  • -
  • Status:
  • Categories: Hold down "Control", or "Command" on a Mac, to select more than one.
  • +
  • Status:
  • # ModelChoiceField ############################################################