Commits

Daniil Bondarev  committed 109dca2 Draft

Added exclude param to get_data_dict and populate_obj.
Excludes field from being populated/added to data_dict

  • Participants
  • Parent commits 66beb65

Comments (0)

Files changed (2)

File tests/form.py

     def test_get_data_dict(self):
         form = self.get_form()
         form.process(test='foobar')
-        data_dict = form.get_data_dict()
-        self.assertEqual(data_dict, {'test': 'foobar'})
+        self.assertEqual(form.get_data_dict(), {'test': 'foobar'})
+        self.assertEqual(form.get_data_dict(excluse=['test']), {})
 
     def test_populate_obj(self):
-        m = type(str('Model'), (object, ), {})
+        m1 = type(str('Model'), (object, ), {})
+        m2 = type(str('Model'), (object, ), {})
         form = self.get_form()
         form.process(test='foobar')
-        form.populate_obj(m)
-        self.assertEqual(m.test, 'foobar')
-        self.assertEqual([k for k in dir(m) if not k.startswith('_')], ['test'])
+        form.populate_obj(m1)
+        self.assertEqual(m1.test, 'foobar')
+        self.assertEqual([k for k in dir(m1) if not k.startswith('_')], ['test'])
+        form.populate_obj(m2, exclude=['test'])
+        self.assertFalse(hasattr(m2, 'test'))
 
     def test_prefixes(self):
         form = self.get_form(prefix='foo')

File wtforms/form.py

         """
         return None
 
-    def get_data_dict(self):
+    def get_data_dict(self, exclude=[]):
         """
         Return a dictionary with data from the form's field. 
+        
+        :param exclude:
+            Used to exclude fields data from returned dictionary.
         """
-        return {name:field.data for name, field in self._fields.iteritems()}
+        return {name:field.data 
+                    for name, field in self._fields.iteritems()
+                    if name not in exclude}
 
-    def populate_obj(self, obj):
+    def populate_obj(self, obj, exclude=[]):
         """
         Populates the attributes of the passed `obj` with data from the form's
         fields.
 
+        :param exclude:
+            Used to exclude fields from populating `obj`.
+
         :note: This is a destructive operation; Any attribute with the same name
                as a field will be overridden. Use with caution.
         """
         for name, field in iteritems(self._fields):
-            field.populate_obj(obj, name)
+            if name not in exclude:
+                field.populate_obj(obj, name)
 
     def process(self, formdata=None, obj=None, **kwargs):
         """