diff --git a/django/forms/models.py b/django/forms/models.py
index 8a48e15..9c1287f 100644
a
|
b
|
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: |
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index 9a962ad..6cd1a72 100644
a
|
b
|
familiar with the mechanics.
|
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: |
… |
… |
First class with a Meta class wins.
|
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). |
… |
… |
fields with the 'choices' attribute are represented by a ChoiceField.
|
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'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'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 |
… |
… |
True
|
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'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'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 | {} |
… |
… |
Add some categories and test the many-to-many form output.
|
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'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'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)]}) |
… |
… |
Initial values can be provided for model forms
|
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'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'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) |
… |
… |
the data in the database when the form is instantiated.
|
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'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'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> |
… |
… |
the data in the database when the form is instantiated.
|
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'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 | |