Ticket #16644: 16644.patch
File 16644.patch, 14.2 KB (added by , 13 years ago) |
---|
-
tests/modeltests/model_forms/tests.py
658 658 <tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr> 659 659 <tr><th>Writer:</th><td><select name="writer"> 660 660 <option value="" selected="selected">---------</option> 661 <option value=" 2">Bob Woodward</option>662 <option value=" 1">Mike Royko</option>661 <option value="%s">Bob Woodward</option> 662 <option value="%s">Mike Royko</option> 663 663 </select></td></tr> 664 664 <tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr> 665 665 <tr><th>Categories:</th><td><select multiple="multiple" name="categories"> 666 <option value=" 1">Entertainment</option>667 <option value=" 2">It's a test</option>668 <option value=" 3">Third test</option>666 <option value="%s">Entertainment</option> 667 <option value="%s">It's a test</option> 668 <option value="%s">Third test</option> 669 669 </select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr> 670 670 <tr><th>Status:</th><td><select name="status"> 671 671 <option value="" selected="selected">---------</option> 672 672 <option value="1">Draft</option> 673 673 <option value="2">Pending</option> 674 674 <option value="3">Live</option> 675 </select></td></tr>''' )675 </select></td></tr>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk)) 676 676 677 677 # You can restrict a form to a subset of the complete list of fields 678 678 # by providing a 'fields' argument. If you try to save a … … 706 706 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 707 707 <li>Writer: <select name="writer"> 708 708 <option value="">---------</option> 709 <option value=" 2">Bob Woodward</option>710 <option value=" 1" selected="selected">Mike Royko</option>709 <option value="%s">Bob Woodward</option> 710 <option value="%s" selected="selected">Mike Royko</option> 711 711 </select></li> 712 712 <li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li> 713 713 <li>Categories: <select multiple="multiple" name="categories"> 714 <option value=" 1">Entertainment</option>715 <option value=" 2">It's a test</option>716 <option value=" 3">Third test</option>714 <option value="%s">Entertainment</option> 715 <option value="%s">It's a test</option> 716 <option value="%s">Third test</option> 717 717 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li> 718 718 <li>Status: <select name="status"> 719 719 <option value="" selected="selected">---------</option> 720 720 <option value="1">Draft</option> 721 721 <option value="2">Pending</option> 722 722 <option value="3">Live</option> 723 </select></li>''' )723 </select></li>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk)) 724 724 f = TestArticleForm({ 725 725 'headline': u'Test headline', 726 726 'slug': 'test-headline', … … 760 760 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 761 761 <li>Writer: <select name="writer"> 762 762 <option value="">---------</option> 763 <option value=" 2">Bob Woodward</option>764 <option value=" 1" selected="selected">Mike Royko</option>763 <option value="%s">Bob Woodward</option> 764 <option value="%s" selected="selected">Mike Royko</option> 765 765 </select></li> 766 766 <li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li> 767 767 <li>Categories: <select multiple="multiple" name="categories"> 768 <option value=" 1" selected="selected">Entertainment</option>769 <option value=" 2">It's a test</option>770 <option value=" 3">Third test</option>768 <option value="%s" selected="selected">Entertainment</option> 769 <option value="%s">It's a test</option> 770 <option value="%s">Third test</option> 771 771 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li> 772 772 <li>Status: <select name="status"> 773 773 <option value="" selected="selected">---------</option> 774 774 <option value="1">Draft</option> 775 775 <option value="2">Pending</option> 776 776 <option value="3">Live</option> 777 </select></li>''' )777 </select></li>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk)) 778 778 779 779 # Initial values can be provided for model forms 780 780 f = TestArticleForm( … … 788 788 <li>Pub date: <input type="text" name="pub_date" /></li> 789 789 <li>Writer: <select name="writer"> 790 790 <option value="" selected="selected">---------</option> 791 <option value=" 2">Bob Woodward</option>792 <option value=" 1">Mike Royko</option>791 <option value="%s">Bob Woodward</option> 792 <option value="%s">Mike Royko</option> 793 793 </select></li> 794 794 <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li> 795 795 <li>Categories: <select multiple="multiple" name="categories"> 796 <option value=" 1" selected="selected">Entertainment</option>797 <option value=" 2" selected="selected">It's a test</option>798 <option value=" 3">Third test</option>796 <option value="%s" selected="selected">Entertainment</option> 797 <option value="%s" selected="selected">It's a test</option> 798 <option value="%s">Third test</option> 799 799 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li> 800 800 <li>Status: <select name="status"> 801 801 <option value="" selected="selected">---------</option> 802 802 <option value="1">Draft</option> 803 803 <option value="2">Pending</option> 804 804 <option value="3">Live</option> 805 </select></li>''' )805 </select></li>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk)) 806 806 807 807 f = TestArticleForm({ 808 808 'headline': u'New headline', … … 882 882 <li>Pub date: <input type="text" name="pub_date" /></li> 883 883 <li>Writer: <select name="writer"> 884 884 <option value="" selected="selected">---------</option> 885 <option value=" 2">Bob Woodward</option>886 <option value=" 1">Mike Royko</option>885 <option value="%s">Bob Woodward</option> 886 <option value="%s">Mike Royko</option> 887 887 </select></li> 888 888 <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li> 889 889 <li>Categories: <select multiple="multiple" name="categories"> 890 <option value=" 1">Entertainment</option>891 <option value=" 2">It's a test</option>892 <option value=" 3">Third</option>890 <option value="%s">Entertainment</option> 891 <option value="%s">It's a test</option> 892 <option value="%s">Third</option> 893 893 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li> 894 894 <li>Status: <select name="status"> 895 895 <option value="" selected="selected">---------</option> 896 896 <option value="1">Draft</option> 897 897 <option value="2">Pending</option> 898 898 <option value="3">Live</option> 899 </select></li>''' )899 </select></li>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk)) 900 900 901 901 c4 = Category.objects.create(name='Fourth', url='4th') 902 902 self.assertEqual(c4.name, 'Fourth') 903 self.assertEqual(Writer.objects.create(name='Carl Bernstein').name, 'Carl Bernstein') 903 w_bernstein = Writer.objects.create(name='Carl Bernstein') 904 self.assertEqual(w_bernstein.name, 'Carl Bernstein') 904 905 self.assertEqual(f.as_ul(), '''<li>Headline: <input type="text" name="headline" maxlength="50" /></li> 905 906 <li>Slug: <input type="text" name="slug" maxlength="50" /></li> 906 907 <li>Pub date: <input type="text" name="pub_date" /></li> 907 908 <li>Writer: <select name="writer"> 908 909 <option value="" selected="selected">---------</option> 909 <option value=" 2">Bob Woodward</option>910 <option value=" 3">Carl Bernstein</option>911 <option value=" 1">Mike Royko</option>910 <option value="%s">Bob Woodward</option> 911 <option value="%s">Carl Bernstein</option> 912 <option value="%s">Mike Royko</option> 912 913 </select></li> 913 914 <li>Article: <textarea rows="10" cols="40" name="article"></textarea></li> 914 915 <li>Categories: <select multiple="multiple" name="categories"> 915 <option value=" 1">Entertainment</option>916 <option value=" 2">It's a test</option>917 <option value=" 3">Third</option>918 <option value=" 4">Fourth</option>916 <option value="%s">Entertainment</option> 917 <option value="%s">It's a test</option> 918 <option value="%s">Third</option> 919 <option value="%s">Fourth</option> 919 920 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li> 920 921 <li>Status: <select name="status"> 921 922 <option value="" selected="selected">---------</option> 922 923 <option value="1">Draft</option> 923 924 <option value="2">Pending</option> 924 925 <option value="3">Live</option> 925 </select></li>''' )926 </select></li>''' % (w_woodward.pk, w_bernstein.pk, w_royko.pk, c1.pk, c2.pk, c3.pk, c4.pk)) 926 927 927 928 # ModelChoiceField ############################################################ 928 929 929 930 f = forms.ModelChoiceField(Category.objects.all()) 930 931 self.assertEqual(list(f.choices), [ 931 932 (u'', u'---------'), 932 ( 1, u'Entertainment'),933 ( 2, u"It's a test"),934 ( 3, u'Third'),935 ( 4, u'Fourth')])933 (c1.pk, u'Entertainment'), 934 (c2.pk, u"It's a test"), 935 (c3.pk, u'Third'), 936 (c4.pk, u'Fourth')]) 936 937 with self.assertRaises(ValidationError): 937 938 f.clean('') 938 939 with self.assertRaises(ValidationError): … … 967 968 f.queryset = Category.objects.exclude(name='Fourth') 968 969 self.assertEqual(list(f.choices), [ 969 970 (u'', u'---------'), 970 ( 1, u'Entertainment'),971 ( 2, u"It's a test"),972 ( 3, u'Third')])971 (c1.pk, u'Entertainment'), 972 (c2.pk, u"It's a test"), 973 (c3.pk, u'Third')]) 973 974 self.assertEqual(f.clean(c3.id).name, 'Third') 974 975 with self.assertRaises(ValidationError): 975 976 f.clean(c4.id) … … 977 978 # check that we can safely iterate choices repeatedly 978 979 gen_one = list(f.choices) 979 980 gen_two = f.choices 980 self.assertEqual(gen_one[2], ( 2, u"It's a test"))981 self.assertEqual(gen_one[2], (c2.pk, u"It's a test")) 981 982 self.assertEqual(list(gen_two), [ 982 983 (u'', u'---------'), 983 ( 1, u'Entertainment'),984 ( 2, u"It's a test"),985 ( 3, u'Third')])984 (c1.pk, u'Entertainment'), 985 (c2.pk, u"It's a test"), 986 (c3.pk, u'Third')]) 986 987 987 988 # check that we can override the label_from_instance method to print custom labels (#4620) 988 989 f.queryset = Category.objects.all() 989 990 f.label_from_instance = lambda obj: "category " + str(obj) 990 991 self.assertEqual(list(f.choices), [ 991 992 (u'', u'---------'), 992 ( 1, 'category Entertainment'),993 ( 2, "category It's a test"),994 ( 3, 'category Third'),995 ( 4, 'category Fourth')])993 (c1.pk, 'category Entertainment'), 994 (c2.pk, "category It's a test"), 995 (c3.pk, 'category Third'), 996 (c4.pk, 'category Fourth')]) 996 997 997 998 # ModelMultipleChoiceField #################################################### 998 999 999 1000 f = forms.ModelMultipleChoiceField(Category.objects.all()) 1000 1001 self.assertEqual(list(f.choices), [ 1001 ( 1, u'Entertainment'),1002 ( 2, u"It's a test"),1003 ( 3, u'Third'),1004 ( 4, u'Fourth')])1002 (c1.pk, u'Entertainment'), 1003 (c2.pk, u"It's a test"), 1004 (c3.pk, u'Third'), 1005 (c4.pk, u'Fourth')]) 1005 1006 with self.assertRaises(ValidationError): 1006 1007 f.clean(None) 1007 1008 with self.assertRaises(ValidationError): … … 1046 1047 # queryset can be changed after the field is created. 1047 1048 f.queryset = Category.objects.exclude(name='Fourth') 1048 1049 self.assertEqual(list(f.choices), [ 1049 ( 1, u'Entertainment'),1050 ( 2, u"It's a test"),1051 ( 3, u'Third')])1050 (c1.pk, u'Entertainment'), 1051 (c2.pk, u"It's a test"), 1052 (c3.pk, u'Third')]) 1052 1053 self.assertEqual(map(lambda o: o.name, f.clean([c3.id])), ["Third"]) 1053 1054 with self.assertRaises(ValidationError): 1054 1055 f.clean([c4.id]) … … 1058 1059 f.queryset = Category.objects.all() 1059 1060 f.label_from_instance = lambda obj: "multicategory " + str(obj) 1060 1061 self.assertEqual(list(f.choices), [ 1061 ( 1, 'multicategory Entertainment'),1062 ( 2, "multicategory It's a test"),1063 ( 3, 'multicategory Third'),1064 ( 4, 'multicategory Fourth')])1062 (c1.pk, 'multicategory Entertainment'), 1063 (c2.pk, "multicategory It's a test"), 1064 (c3.pk, 'multicategory Third'), 1065 (c4.pk, 'multicategory Fourth')]) 1065 1066 1066 1067 # OneToOneField ############################################################### 1067 1068 … … 1082 1083 form = WriterProfileForm() 1083 1084 self.assertEqual(form.as_p(), '''<p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer"> 1084 1085 <option value="" selected="selected">---------</option> 1085 <option value=" 2">Bob Woodward</option>1086 <option value=" 3">Carl Bernstein</option>1087 <option value=" 4">Joe Better</option>1088 <option value=" 1">Mike Royko</option>1086 <option value="%s">Bob Woodward</option> 1087 <option value="%s">Carl Bernstein</option> 1088 <option value="%s">Joe Better</option> 1089 <option value="%s">Mike Royko</option> 1089 1090 </select></p> 1090 <p><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>''' )1091 <p><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>''' % (w_woodward.pk, w_bernstein.pk, bw.pk, w_royko.pk)) 1091 1092 1092 1093 data = { 1093 1094 'writer': unicode(w_woodward.pk), … … 1100 1101 form = WriterProfileForm(instance=instance) 1101 1102 self.assertEqual(form.as_p(), '''<p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer"> 1102 1103 <option value="">---------</option> 1103 <option value=" 2" selected="selected">Bob Woodward</option>1104 <option value=" 3">Carl Bernstein</option>1105 <option value=" 4">Joe Better</option>1106 <option value=" 1">Mike Royko</option>1104 <option value="%s" selected="selected">Bob Woodward</option> 1105 <option value="%s">Carl Bernstein</option> 1106 <option value="%s">Joe Better</option> 1107 <option value="%s">Mike Royko</option> 1107 1108 </select></p> 1108 <p><label for="id_age">Age:</label> <input type="text" name="age" value="65" id="id_age" /></p>''' )1109 <p><label for="id_age">Age:</label> <input type="text" name="age" value="65" id="id_age" /></p>''' % (w_woodward.pk, w_bernstein.pk, bw.pk, w_royko.pk)) 1109 1110 1110 1111 def test_phone_number_field(self): 1111 1112 f = PhoneNumberForm({'phone': '(312) 555-1212', 'description': 'Assistance'})