Changeset 4300
- Timestamp:
- 01/08/07 23:49:47 (2 years ago)
- Files:
-
- django/trunk/django/newforms/models.py (modified) (5 diffs)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/models.py
r4299 r4300 6 6 from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList 7 7 8 __all__ = (' form_for_model', 'form_for_instance', 'form_for_fields')8 __all__ = ('save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields') 9 9 10 10 def model_save(self, commit=True): … … 21 21 return obj 22 22 23 def make_instance_save(opts, instance): 23 def save_instance(form, instance, commit=True): 24 """ 25 Saves bound Form ``form``'s clean_data into model instance ``instance``. 26 27 Assumes ``form`` has a field for every non-AutoField database field in 28 ``instance``. If commit=True, then the changes to ``instance`` will be 29 saved to the database. Returns ``instance``. 30 """ 31 from django.db import models 32 opts = instance.__class__._meta 33 if form.errors: 34 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 35 clean_data = form.clean_data 36 for f in opts.fields + opts.many_to_many: 37 if isinstance(f, models.AutoField): 38 continue 39 setattr(instance, f.attname, clean_data[f.name]) 40 if commit: 41 instance.save() 42 return instance 43 44 def make_instance_save(instance): 24 45 "Returns the save() method for a form_for_instance Form." 25 from django.db import models 26 def apply_changes(self, commit=True): 27 if self.errors: 28 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 29 clean_data = self.clean_data 30 for f in opts.fields + opts.many_to_many: 31 if isinstance(f, models.AutoField): 32 continue 33 setattr(instance, f.attname, clean_data[f.name]) 34 if commit: 35 instance.save() 36 return instance 37 return apply_changes 46 def save(self, commit=True): 47 return save_instance(self, instance, commit) 48 return save 38 49 39 50 def form_for_model(model, form=BaseForm): … … 41 52 Returns a Form class for the given Django model class. 42 53 43 Provide 'form'if you want to use a custom BaseForm subclass.54 Provide ``form`` if you want to use a custom BaseForm subclass. 44 55 """ 45 56 opts = model._meta … … 56 67 Returns a Form class for the given Django model instance. 57 68 58 Provide 'form'if you want to use a custom BaseForm subclass.69 Provide ``form`` if you want to use a custom BaseForm subclass. 59 70 """ 60 71 model = instance.__class__ … … 68 79 fields = SortedDictFromList(field_list) 69 80 return type(opts.object_name + 'InstanceForm', (form,), 70 {'fields': fields, '_model': model, 'save': make_instance_save( opts,instance)})81 {'fields': fields, '_model': model, 'save': make_instance_save(instance)}) 71 82 72 83 def form_for_fields(field_list): django/trunk/tests/modeltests/model_forms/models.py
r4299 r4300 16 16 returns a Form that is tied to the instance. This form works just like any 17 17 other Form, with one additional method: save(). The save() 18 method updates the model instance. It saves the changes to the database if 19 save(commit=True), which is default. If you pass commit=False, then you'll 20 get the object without committing the changes to the database. 18 method updates the model instance. It also takes a commit=True parameter. 19 20 The function django.newforms.save_instance() takes a bound form instance and a 21 model instance and saves the form's clean_data into the instance. It also takes 22 a commit=True parameter. 21 23 """ 22 24 … … 46 48 47 49 __test__ = {'API_TESTS': """ 48 >>> from django.newforms import form_for_model, form_for_instance, BaseForm50 >>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField 49 51 >>> import datetime 50 52 … … 219 221 </select></li> 220 222 223 Here, we define a custom Form. Because it happens to have the same fields as 224 the Category model, we can use save_instance() to apply its changes to an 225 existing Category instance. 226 >>> class ShortCategory(Form): 227 ... name = CharField(max_length=5) 228 ... url = CharField(max_length=3) 229 >>> cat = Category.objects.get(name='Third test') 230 >>> cat 231 <Category: Third test> 232 >>> cat.id 233 3 234 >>> sc = ShortCategory({'name': 'Third', 'url': '3rd'}) 235 >>> save_instance(sc, cat) 236 <Category: Third> 237 >>> Category.objects.get(id=3) 238 <Category: Third> 221 239 """}
