Changeset 4299
- Timestamp:
- 01/08/07 23:22:48 (2 years ago)
- Files:
-
- django/trunk/django/newforms/models.py (modified) (5 diffs)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/models.py
r4265 r4299 8 8 __all__ = ('form_for_model', 'form_for_instance', 'form_for_fields') 9 9 10 def create(self, save=True):10 def model_save(self, commit=True): 11 11 """ 12 12 Creates and returns model instance according to self.clean_data. … … 17 17 raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) 18 18 obj = self._model(**self.clean_data) 19 if save:19 if commit: 20 20 obj.save() 21 21 return obj 22 22 23 def make_ apply_changes(opts, instance):24 "Returns the apply_changes() method for a form_for_instance Form."23 def make_instance_save(opts, instance): 24 "Returns the save() method for a form_for_instance Form." 25 25 from django.db import models 26 def apply_changes(self, save=True):26 def apply_changes(self, commit=True): 27 27 if self.errors: 28 28 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) … … 32 32 continue 33 33 setattr(instance, f.attname, clean_data[f.name]) 34 if save:34 if commit: 35 35 instance.save() 36 36 return instance … … 50 50 field_list.append((f.name, formfield)) 51 51 fields = SortedDictFromList(field_list) 52 return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, ' create': create})52 return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'save': model_save}) 53 53 54 54 def form_for_instance(instance, form=BaseForm): … … 68 68 fields = SortedDictFromList(field_list) 69 69 return type(opts.object_name + 'InstanceForm', (form,), 70 {'fields': fields, '_model': model, ' apply_changes': make_apply_changes(opts, instance)})70 {'fields': fields, '_model': model, 'save': make_instance_save(opts, instance)}) 71 71 72 72 def form_for_fields(field_list): django/trunk/tests/modeltests/model_forms/models.py
r4294 r4299 7 7 The function django.newforms.form_for_model() takes a model class and returns 8 8 a Form that is tied to the model. This Form works just like any other Form, 9 with one additional method: create(). The create() method creates an instance9 with one additional method: save(). The save() method creates an instance 10 10 of the model and returns that newly created instance. It saves the instance to 11 the database if create(save=True), which is default. If you pass 12 create(save=False), then you'll get the object without saving it. 11 the database if save(commit=True), which is default. If you pass 12 commit=False, then you'll get the object without committing the changes to the 13 database. 13 14 14 15 The function django.newforms.form_for_instance() takes a model instance and 15 16 returns a Form that is tied to the instance. This form works just like any 16 other Form, with one additional method: apply_changes(). The apply_changes()17 other Form, with one additional method: save(). The save() 17 18 method updates the model instance. It saves the changes to the database if 18 apply_changes(save=True), which is default. If you pass save=False, then you'll19 get the object without saving it.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. 20 21 """ 21 22 … … 72 73 >>> f.clean_data 73 74 {'url': u'entertainment', 'name': u'Entertainment'} 74 >>> obj = f. create()75 >>> obj = f.save() 75 76 >>> obj 76 77 <Category: Entertainment> … … 83 84 >>> f.clean_data 84 85 {'url': u'test', 'name': u"It's a test"} 85 >>> obj = f. create()86 >>> obj = f.save() 86 87 >>> obj 87 88 <Category: It's a test> … … 89 90 [<Category: Entertainment>, <Category: It's a test>] 90 91 91 If you call create() with save=False, then it will return an object that hasn't 92 yet been saved. In this case, it's up to you to save it. 92 If you call save() with commit=False, then it will return an object that 93 hasn't yet been saved to the database. In this case, it's up to you to call 94 save() on the resulting model instance. 93 95 >>> f = CategoryForm({'name': 'Third test', 'url': 'third'}) 94 96 >>> f.is_valid() … … 96 98 >>> f.clean_data 97 99 {'url': u'third', 'name': u'Third test'} 98 >>> obj = f. create(save=False)100 >>> obj = f.save(commit=False) 99 101 >>> obj 100 102 <Category: Third test> … … 105 107 [<Category: Entertainment>, <Category: It's a test>, <Category: Third test>] 106 108 107 If you call create() with invalid data, you'll get a ValueError.109 If you call save() with invalid data, you'll get a ValueError. 108 110 >>> f = CategoryForm({'name': '', 'url': 'foo'}) 109 111 >>> f.errors … … 113 115 ... 114 116 AttributeError: 'CategoryForm' object has no attribute 'clean_data' 115 >>> f. create()117 >>> f.save() 116 118 Traceback (most recent call last): 117 119 ... 118 120 ValueError: The Category could not be created because the data didn't validate. 119 121 >>> f = CategoryForm({'name': '', 'url': 'foo'}) 120 >>> f. create()122 >>> f.save() 121 123 Traceback (most recent call last): 122 124 ... … … 157 159 hello 158 160 159 Use form_for_instance to create a Form from a model instance. There are two 160 differences between this Form and one created via form_for_model. First, the 161 object's current values are inserted as 'initial' data in each Field. Second, 162 the Form gets an apply_changes() method instead of a create() method. 161 Use form_for_instance to create a Form from a model instance. The difference 162 between this Form and one created via form_for_model is that the object's 163 current values are inserted as 'initial' data in each Field. 163 164 >>> w = Writer.objects.get(name='Mike Royko') 164 165 >>> RoykoForm = form_for_instance(w) … … 189 190 >>> f.is_valid() 190 191 True 191 >>> new_art = f. apply_changes()192 >>> new_art = f.save() 192 193 >>> new_art.id 193 194 1
