Ticket #3186: django_newforms_from_db_helper2.patch
File django_newforms_from_db_helper2.patch, 1.2 KB (added by , 18 years ago) |
---|
-
django/newforms/models.py
5 5 6 6 from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList 7 7 8 __all__ = ('form_for_model', 'form_for_fields' )8 __all__ = ('form_for_model', 'form_for_fields', 'form_with_db_data') 9 9 10 10 def create(self, save=True): 11 11 "Creates and returns model instance according to self.clean_data." … … 36 36 "Returns a Form class for the given list of Django database field instances." 37 37 fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list]) 38 38 return type('FormForFields', (BaseForm,), {'fields': fields}) 39 40 def form_with_db_data(obj, form, **kwargs): 41 """Return an instance of the given Form class whose fields are 42 pre-filled with data from the given SQL object.""" 43 44 data = {} 45 for field in form.fields.keys(): 46 data[field] = getattr(obj, field, None) 47 f = form(data=data, **kwargs) 48 # Force the form to ignore errors, as we want this prefilled form 49 # returned to the user, not immediately rolled back into the 50 # database. 51 f.ignore_errors = True 52 return f