| 864 | |
| 865 | # Tests for declarative modelformset |
| 866 | |
| 867 | # clean up first |
| 868 | >>> Author.objects.all().delete() |
| 869 | |
| 870 | >>> class AuthorForm(forms.ModelForm): |
| 871 | ... class Meta: |
| 872 | ... model = Author |
| 873 | |
| 874 | # test definition checks |
| 875 | >>> class BadFormSet1(forms.models.ModelFormSet): |
| 876 | ... form = AuthorForm |
| 877 | Traceback (most recent call last): |
| 878 | ... |
| 879 | Exception: Error in definition of ModelFormSet subclass BadFormSet1. You need to define a model. |
| 880 | |
| 881 | >>> class BadFormSet2(forms.models.ModelFormSet): |
| 882 | ... model = Author |
| 883 | Traceback (most recent call last): |
| 884 | ... |
| 885 | Exception: Error in definition of ModelFormSet subclass BadFormSet2. You need to define a form. |
| 886 | |
| 887 | # test the modelformset functionality |
| 888 | >>> class DeclarativeAuthorFormSet(forms.models.ModelFormSet): |
| 889 | ... form = AuthorForm |
| 890 | ... model = Author |
| 891 | ... extra = 3 |
| 892 | |
| 893 | >>> qs = Author.objects.all() |
| 894 | >>> formset = DeclarativeAuthorFormSet(queryset=qs) |
| 895 | >>> for form in formset.forms: |
| 896 | ... print form.as_p() |
| 897 | <p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /><input type="hidden" name="form-0-id" id="id_form-0-id" /></p> |
| 898 | <p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /><input type="hidden" name="form-1-id" id="id_form-1-id" /></p> |
| 899 | <p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /><input type="hidden" name="form-2-id" id="id_form-2-id" /></p> |
| 900 | |
| 901 | >>> data = { |
| 902 | ... 'form-TOTAL_FORMS': '3', # the number of forms rendered |
| 903 | ... 'form-INITIAL_FORMS': '0', # the number of forms with initial data |
| 904 | ... 'form-0-name': 'Charles Baudelaire', |
| 905 | ... 'form-1-name': 'Arthur Rimbaud', |
| 906 | ... 'form-2-name': '', |
| 907 | ... } |
| 908 | |
| 909 | >>> formset = DeclarativeAuthorFormSet(data=data, queryset=qs) |
| 910 | >>> formset.is_valid() |
| 911 | True |
| 912 | |
| 913 | >>> formset.save() |
| 914 | [<Author: Charles Baudelaire>, <Author: Arthur Rimbaud>] |
| 915 | |
| 916 | >>> for author in Author.objects.order_by('name'): |
| 917 | ... print author.name |
| 918 | Arthur Rimbaud |
| 919 | Charles Baudelaire |
| 920 | |
| 921 | |
| 922 | |
| 923 | # Tests for declarative inlineformset |
| 924 | |
| 925 | # clean up first |
| 926 | >>> Book.objects.all().delete() |
| 927 | >>> author = Author.objects.get(pk=1) |
| 928 | >>> class BookForm(forms.ModelForm): |
| 929 | ... class Meta: |
| 930 | ... model = Book |
| 931 | |
| 932 | # test definition checks |
| 933 | >>> class BadFormSet1(forms.models.InlineFormSet): |
| 934 | ... form = BookForm |
| 935 | ... parent = 'author' |
| 936 | Traceback (most recent call last): |
| 937 | ... |
| 938 | Exception: Error in definition of InlineFormSet subclass BadFormSet1. You need to define a model. |
| 939 | |
| 940 | >>> class BadFormSet2(forms.models.InlineFormSet): |
| 941 | ... model = Book |
| 942 | ... parent = 'author' |
| 943 | Traceback (most recent call last): |
| 944 | ... |
| 945 | Exception: Error in definition of InlineFormSet subclass BadFormSet2. You need to define a form. |
| 946 | |
| 947 | >>> class BadFormSet3(forms.models.InlineFormSet): |
| 948 | ... model = Book |
| 949 | ... form = BookForm |
| 950 | Traceback (most recent call last): |
| 951 | ... |
| 952 | Exception: Error in definition of InlineFormSet subclass BadFormSet3. You need to define a parent attribute. |
| 953 | |
| 954 | >>> class BadFormSet4(forms.models.InlineFormSet): |
| 955 | ... model = Book |
| 956 | ... form = BookForm |
| 957 | ... parent = 'unexistingfield' |
| 958 | Traceback (most recent call last): |
| 959 | ... |
| 960 | Exception: Error in definition of InlineFormSet subclass BadFormSet4. The parent attribute of the inlineformset is not a valid field. |
| 961 | |
| 962 | >>> class BadFormSet5(forms.models.InlineFormSet): |
| 963 | ... model = Book |
| 964 | ... form = BookForm |
| 965 | ... parent = 'title' |
| 966 | Traceback (most recent call last): |
| 967 | ... |
| 968 | Exception: Error in definition of InlineFormSet subclass BadFormSet5. The parent attribute is not a ForeignKey. |
| 969 | |
| 970 | # test the inlineformset functionality |
| 971 | >>> class DeclarativeAuthorBooksFormSet(forms.models.InlineFormSet): |
| 972 | ... model = Book |
| 973 | ... form = BookForm |
| 974 | ... parent = 'author' |
| 975 | |
| 976 | >>> formset = DeclarativeAuthorBooksFormSet(instance=author) |
| 977 | >>> for form in formset.forms: |
| 978 | ... print form.as_p() |
| 979 | <p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" id="id_book_set-0-id" /></p> |
| 980 | <p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p> |
| 981 | <p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p> |
| 982 | |
| 983 | >>> data = { |
| 984 | ... 'book_set-TOTAL_FORMS': '3', # the number of forms rendered |
| 985 | ... 'book_set-INITIAL_FORMS': '0', # the number of forms with initial data |
| 986 | ... 'book_set-0-title': 'Les Fleurs du Mal', |
| 987 | ... 'book_set-1-title': '', |
| 988 | ... 'book_set-2-title': '', |
| 989 | ... } |
| 990 | |
| 991 | >>> formset = DeclarativeAuthorBooksFormSet(data, instance=author) |
| 992 | >>> formset.is_valid() |
| 993 | True |
| 994 | |
| 995 | >>> formset.save() |
| 996 | [<Book: Les Fleurs du Mal>] |
| 997 | |
| 998 | >>> for book in author.book_set.all(): |
| 999 | ... print book.title |
| 1000 | Les Fleurs du Mal |
| 1001 | |
| 1002 | >>> formset = DeclarativeAuthorBooksFormSet(instance=author) |
| 1003 | >>> for form in formset.forms: |
| 1004 | ... print form.as_p() |
| 1005 | <p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id" /></p> |
| 1006 | <p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p> |
| 1007 | <p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p> |
| 1008 | <p><label for="id_book_set-3-title">Title:</label> <input id="id_book_set-3-title" type="text" name="book_set-3-title" maxlength="100" /><input type="hidden" name="book_set-3-author" value="1" id="id_book_set-3-author" /><input type="hidden" name="book_set-3-id" id="id_book_set-3-id" /></p> |
| 1009 | |
| 1010 | >>> data = { |
| 1011 | ... 'book_set-TOTAL_FORMS': '3', # the number of forms rendered |
| 1012 | ... 'book_set-INITIAL_FORMS': '1', # the number of forms with initial data |
| 1013 | ... 'book_set-0-id': '1', |
| 1014 | ... 'book_set-0-title': 'Les Fleurs du Mal', |
| 1015 | ... 'book_set-1-title': 'Le Spleen de Paris', |
| 1016 | ... 'book_set-2-title': '', |
| 1017 | ... 'book_set-3-title': '', |
| 1018 | ... } |
| 1019 | |
| 1020 | >>> formset = DeclarativeAuthorBooksFormSet(data, instance=author) |
| 1021 | >>> formset.is_valid() |
| 1022 | True |
| 1023 | |
| 1024 | >>> formset.save() |
| 1025 | [<Book: Le Spleen de Paris>] |
| 1026 | |
| 1027 | |
| 1028 | # specific declarative field definitions just work on the declarative form for formsets |
| 1029 | # no need to define the widget in __init__ |
| 1030 | >>> class MembershipForm(forms.ModelForm): |
| 1031 | ... date_joined = forms.SplitDateTimeField(initial=now, widget=forms.SplitDateTimeWidget) |
| 1032 | ... class Meta: |
| 1033 | ... model = Membership |
| 1034 | |
| 1035 | >>> class FormSet(forms.models.InlineFormSet): |
| 1036 | ... model = Membership |
| 1037 | ... form = MembershipForm |
| 1038 | ... parent = 'person' |
| 1039 | ... extra = 1 |
| 1040 | >>> data = { |
| 1041 | ... 'membership_set-TOTAL_FORMS': '1', |
| 1042 | ... 'membership_set-INITIAL_FORMS': '0', |
| 1043 | ... 'membership_set-0-date_joined_0': unicode(now.strftime('%Y-%m-%d')), |
| 1044 | ... 'membership_set-0-date_joined_1': unicode(now.strftime('%H:%M:%S')), |
| 1045 | ... 'initial-membership_set-0-date_joined': unicode(now.strftime('%Y-%m-%d %H:%M:%S')), |
| 1046 | ... 'membership_set-0-karma': '', |
| 1047 | ... } |
| 1048 | >>> formset = FormSet(data, instance=person) |
| 1049 | >>> formset.is_valid() |
| 1050 | True |
| 1051 | |