Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 7d11c30994c5f90603264c6e2fcf1d9c79ab8924 1 parent 327081f
Jannis Leidel authored April 22, 2011
2  django/forms/models.py
@@ -143,7 +143,7 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_c
143 143
     field_list = []
144 144
     ignored = []
145 145
     opts = model._meta
146  
-    for f in opts.fields + opts.many_to_many:
  146
+    for f in sorted(opts.fields + opts.many_to_many):
147 147
         if not f.editable:
148 148
             continue
149 149
         if fields is not None and not f.name in fields:
66  tests/modeltests/model_forms/models.py
@@ -371,7 +371,7 @@ class FlexibleDatePost(models.Model):
371 371
 OddForm is now an Article-related thing, because BadForm.Meta overrides
372 372
 CategoryForm.Meta.
373 373
 >>> OddForm.base_fields.keys()
374  
-['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
  374
+['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
375 375
 
376 376
 >>> class ArticleForm(ModelForm):
377 377
 ...     class Meta:
@@ -382,7 +382,7 @@ class FlexibleDatePost(models.Model):
382 382
 >>> class BadForm(ArticleForm, CategoryForm):
383 383
 ...     pass
384 384
 >>> OddForm.base_fields.keys()
385  
-['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
  385
+['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
386 386
 
387 387
 Subclassing without specifying a Meta on the class will use the parent's Meta
388 388
 (or the first parent in the MRO if there are multiple parent classes).
@@ -556,17 +556,17 @@ class FlexibleDatePost(models.Model):
556 556
 <option value="...">Mike Royko</option>
557 557
 </select></td></tr>
558 558
 <tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr>
  559
+<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
  560
+<option value="...">Entertainment</option>
  561
+<option value="...">It&#39;s a test</option>
  562
+<option value="...">Third test</option>
  563
+</select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>
559 564
 <tr><th>Status:</th><td><select name="status">
560 565
 <option value="" selected="selected">---------</option>
561 566
 <option value="1">Draft</option>
562 567
 <option value="2">Pending</option>
563 568
 <option value="3">Live</option>
564 569
 </select></td></tr>
565  
-<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
566  
-<option value="...">Entertainment</option>
567  
-<option value="...">It&#39;s a test</option>
568  
-<option value="...">Third test</option>
569  
-</select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>
570 570
 
571 571
 You can restrict a form to a subset of the complete list of fields
572 572
 by providing a 'fields' argument. If you try to save a
@@ -612,17 +612,17 @@ class FlexibleDatePost(models.Model):
612 612
 <option value="..." selected="selected">Mike Royko</option>
613 613
 </select></li>
614 614
 <li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
  615
+<li>Categories: <select multiple="multiple" name="categories">
  616
+<option value="...">Entertainment</option>
  617
+<option value="...">It&#39;s a test</option>
  618
+<option value="...">Third test</option>
  619
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
615 620
 <li>Status: <select name="status">
616 621
 <option value="" selected="selected">---------</option>
617 622
 <option value="1">Draft</option>
618 623
 <option value="2">Pending</option>
619 624
 <option value="3">Live</option>
620 625
 </select></li>
621  
-<li>Categories: <select multiple="multiple" name="categories">
622  
-<option value="...">Entertainment</option>
623  
-<option value="...">It&#39;s a test</option>
624  
-<option value="...">Third test</option>
625  
-</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
626 626
 >>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': unicode(w_royko.pk), 'article': 'Hello.'}, instance=art)
627 627
 >>> f.errors
628 628
 {}
@@ -675,17 +675,17 @@ class FlexibleDatePost(models.Model):
675 675
 <option value="..." selected="selected">Mike Royko</option>
676 676
 </select></li>
677 677
 <li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
  678
+<li>Categories: <select multiple="multiple" name="categories">
  679
+<option value="..." selected="selected">Entertainment</option>
  680
+<option value="...">It&#39;s a test</option>
  681
+<option value="...">Third test</option>
  682
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
678 683
 <li>Status: <select name="status">
679 684
 <option value="" selected="selected">---------</option>
680 685
 <option value="1">Draft</option>
681 686
 <option value="2">Pending</option>
682 687
 <option value="3">Live</option>
683 688
 </select></li>
684  
-<li>Categories: <select multiple="multiple" name="categories">
685  
-<option value="..." selected="selected">Entertainment</option>
686  
-<option value="...">It&#39;s a test</option>
687  
-<option value="...">Third test</option>
688  
-</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
689 689
 
690 690
 Initial values can be provided for model forms
691 691
 >>> f = TestArticleForm(auto_id=False, initial={'headline': 'Your headline here', 'categories': [str(c1.id), str(c2.id)]})
@@ -699,17 +699,17 @@ class FlexibleDatePost(models.Model):
699 699
 <option value="...">Mike Royko</option>
700 700
 </select></li>
701 701
 <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
  702
+<li>Categories: <select multiple="multiple" name="categories">
  703
+<option value="..." selected="selected">Entertainment</option>
  704
+<option value="..." selected="selected">It&#39;s a test</option>
  705
+<option value="...">Third test</option>
  706
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
702 707
 <li>Status: <select name="status">
703 708
 <option value="" selected="selected">---------</option>
704 709
 <option value="1">Draft</option>
705 710
 <option value="2">Pending</option>
706 711
 <option value="3">Live</option>
707 712
 </select></li>
708  
-<li>Categories: <select multiple="multiple" name="categories">
709  
-<option value="..." selected="selected">Entertainment</option>
710  
-<option value="..." selected="selected">It&#39;s a test</option>
711  
-<option value="...">Third test</option>
712  
-</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
713 713
 
714 714
 >>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04',
715 715
 ...     'writer': unicode(w_royko.pk), 'article': u'Hello.', 'categories': [unicode(c1.id), unicode(c2.id)]}, instance=new_art)
@@ -818,17 +818,17 @@ class FlexibleDatePost(models.Model):
818 818
 <option value="...">Mike Royko</option>
819 819
 </select></li>
820 820
 <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
  821
+<li>Categories: <select multiple="multiple" name="categories">
  822
+<option value="...">Entertainment</option>
  823
+<option value="...">It&#39;s a test</option>
  824
+<option value="...">Third</option>
  825
+</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
821 826
 <li>Status: <select name="status">
822 827
 <option value="" selected="selected">---------</option>
823 828
 <option value="1">Draft</option>
824 829
 <option value="2">Pending</option>
825 830
 <option value="3">Live</option>
826 831
 </select></li>
827  
-<li>Categories: <select multiple="multiple" name="categories">
828  
-<option value="...">Entertainment</option>
829  
-<option value="...">It&#39;s a test</option>
830  
-<option value="...">Third</option>
831  
-</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
832 832
 >>> c4 = Category.objects.create(name='Fourth', url='4th')
833 833
 >>> c4
834 834
 <Category: Fourth>
@@ -845,18 +845,18 @@ class FlexibleDatePost(models.Model):
845 845
 <option value="...">Mike Royko</option>
846 846
 </select></li>
847 847
 <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
848  
-<li>Status: <select name="status">
849  
-<option value="" selected="selected">---------</option>
850  
-<option value="1">Draft</option>
851  
-<option value="2">Pending</option>
852  
-<option value="3">Live</option>
853  
-</select></li>
854 848
 <li>Categories: <select multiple="multiple" name="categories">
855 849
 <option value="...">Entertainment</option>
856 850
 <option value="...">It&#39;s a test</option>
857 851
 <option value="...">Third</option>
858 852
 <option value="...">Fourth</option>
859 853
 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
  854
+<li>Status: <select name="status">
  855
+<option value="" selected="selected">---------</option>
  856
+<option value="1">Draft</option>
  857
+<option value="2">Pending</option>
  858
+<option value="3">Live</option>
  859
+</select></li>
860 860
 
861 861
 # ModelChoiceField ############################################################
862 862
 

0 notes on commit 7d11c30

Please sign in to comment.
Something went wrong with that request. Please try again.