Code

Ticket #17650: baseform_contains.diff

File baseform_contains.diff, 1.3 KB (added by etox, 2 years ago)
Line 
1Index: django/forms/forms.py
2===================================================================
3--- django/forms/forms.py       (revision 17460)
4+++ django/forms/forms.py       (working copy)
5@@ -101,6 +101,9 @@
6         for name in self.fields:
7             yield self[name]
8 
9+    def __contains__(self, name):
10+        return name in self.fields
11+
12     def __getitem__(self, name):
13         "Returns a BoundField with the given name."
14         try:
15Index: tests/regressiontests/forms/tests/forms.py
16===================================================================
17--- tests/regressiontests/forms/tests/forms.py  (revision 17460)
18+++ tests/regressiontests/forms/tests/forms.py  (working copy)
19@@ -42,6 +42,8 @@
20         self.assertHTMLEqual(str(p['first_name']), '<input type="text" name="first_name" value="John" id="id_first_name" />')
21         self.assertHTMLEqual(str(p['last_name']), '<input type="text" name="last_name" value="Lennon" id="id_last_name" />')
22         self.assertHTMLEqual(str(p['birthday']), '<input type="text" name="birthday" value="1940-10-9" id="id_birthday" />')
23+        self.assertTrue('first_name' in p)
24+        self.assertFalse('nonexistentfield' in p)
25         try:
26             p['nonexistentfield']
27             self.fail('Attempts to access non-existent fields should fail.')