Code

Ticket #5628: 5628_formset_errors_fix.diff

File 5628_formset_errors_fix.diff, 3.2 KB (added by brosner, 7 years ago)
Line 
1=== django/newforms/formsets.py
2==================================================================
3--- django/newforms/formsets.py (revision 448)
4+++ django/newforms/formsets.py (patch d5628_fix level 1)
5@@ -87,7 +87,7 @@
6                 change_form = FormClass(**kwargs)
7                 self.add_fields(change_form, i)
8                 change_forms.append(change_form)
9-            self._change_forms= change_forms
10+            self._change_forms = change_forms
11         return self._change_forms
12     change_forms = property(_get_change_forms)
13 
14=== django/contrib/admin/options.py
15==================================================================
16--- django/contrib/admin/options.py     (revision 448)
17+++ django/contrib/admin/options.py     (patch d5628_fix level 1)
18@@ -521,7 +521,7 @@
19             'is_popup': request.REQUEST.has_key('_popup'),
20             'show_delete': False,
21             'media': media,
22-            'inline_admin_formsets': inline_admin_formsets,
23+            'inline_admin_formsets': InlineAdminFormSetList(inline_admin_formsets),
24         })
25         return self.render_change_form(model, c, add=True)
26     
27@@ -596,7 +596,7 @@
28             'original': obj,
29             'is_popup': request.REQUEST.has_key('_popup'),
30             'media': media,
31-            'inline_admin_formsets': inline_admin_formsets,
32+            'inline_admin_formsets': InlineAdminFormSetList(inline_admin_formsets),
33         })
34         return self.render_change_form(model, c, change=True)
35 
36@@ -766,6 +766,26 @@
37 class TabularInline(InlineModelAdmin):
38     template = 'admin/edit_inline/tabular.html'
39 
40+class InlineAdminFormSetList(object):
41+    def __init__(self, admin_formsets):
42+        self.admin_formsets = admin_formsets
43+        self.formsets = [ifs.formset for ifs in admin_formsets]
44+   
45+    def __iter__(self):
46+        return iter(self.admin_formsets)
47+   
48+    def _get_errors(self):
49+        errors = []
50+        for formset in self.formsets:
51+            # HACK: a FormSet object does not always have an errors attribute.
52+            # this will work without the hasattr check for errors since its only
53+            # usage is in the template and variable resolution will suppress the
54+            # AttributeError exception thrown.
55+            if hasattr(formset, 'errors'):
56+                errors.append(formset.errors)
57+        return bool(errors)
58+    errors = property(_get_errors)
59+
60 class InlineAdminFormSet(object):
61     """
62     A wrapper around an inline formset for use in the admin system.
63=== django/contrib/admin/templates/admin/change_form.html
64==================================================================
65--- django/contrib/admin/templates/admin/change_form.html       (revision 448)
66+++ django/contrib/admin/templates/admin/change_form.html       (patch d5628_fix level 1)
67@@ -34,7 +34,7 @@
68 <div>
69 {% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
70 {% if save_on_top %}{% submit_row %}{% endif %}
71-{% if adminform.form.errors %}
72+{% if adminform.form.errors or inline_admin_formsets.errors %}
73     <p class="errornote">
74     {% blocktrans count adminform.form.errors.items|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
75     </p>