Changeset 4253
- Timestamp:
- 12/27/06 20:34:53 (2 years ago)
- Files:
-
- django/trunk/django/newforms/models.py (modified) (2 diffs)
- django/trunk/tests/modeltests/model_forms/models.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/models.py
r4250 r4253 20 20 obj.save() 21 21 return obj 22 23 def make_apply_changes(opts, instance): 24 "Returns the apply_changes() method for a form_for_instance Form." 25 from django.db import models 26 def apply_changes(self, save=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 save: 35 instance.save() 36 return instance 37 return apply_changes 22 38 23 39 def form_for_model(model, form=BaseForm): … … 46 62 field_list = [] 47 63 for f in opts.fields + opts.many_to_many: 48 current_value = getattr(instance, f.attname)64 current_value = f.value_from_object(instance) 49 65 formfield = f.formfield(initial=current_value) 50 66 if formfield: 51 67 field_list.append((f.name, formfield)) 52 68 fields = SortedDictFromList(field_list) 53 return type(opts.object_name + 'InstanceForm', (form,), {'fields': fields, '_model': model}) 69 return type(opts.object_name + 'InstanceForm', (form,), 70 {'fields': fields, '_model': model, 'apply_changes': make_apply_changes(opts, instance)}) 54 71 55 72 def form_for_fields(field_list): django/trunk/tests/modeltests/model_forms/models.py
r4250 r4253 31 31 pub_date = models.DateTimeField() 32 32 writer = models.ForeignKey(Writer) 33 categories = models.ManyToManyField(Category )33 categories = models.ManyToManyField(Category, blank=True) 34 34 35 35 def __str__(self): … … 38 38 __test__ = {'API_TESTS': """ 39 39 >>> from django.newforms import form_for_model, form_for_instance, BaseForm 40 >>> import datetime 40 41 41 42 >>> Category.objects.all() … … 143 144 hello 144 145 145 Use form_for_instance to create a Form from a model instance. The difference 146 between this Form and one created via form_for_model is that the object's 147 current values are inserted as 'initial' data in each Field. 146 Use form_for_instance to create a Form from a model instance. There are two 147 differences between this Form and one created via form_for_model. First, the 148 object's current values are inserted as 'initial' data in each Field. Second, 149 the Form gets an apply_changes() method instead of a create() method. 148 150 >>> w = Writer.objects.get(name='Mike Royko') 149 151 >>> RoykoForm = form_for_instance(w) … … 151 153 >>> print f 152 154 <tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /></td></tr> 155 156 >>> art = Article(headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w) 157 >>> art.save() 158 >>> art.id 159 1 160 >>> TestArticleForm = form_for_instance(art) 161 >>> f = TestArticleForm(auto_id=False) 162 >>> print f.as_ul() 163 <li>Headline: <input type="text" name="headline" value="Test article" maxlength="50" /></li> 164 <li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 165 <li>Writer: <select name="writer"> 166 <option value="">---------</option> 167 <option value="1" selected="selected">Mike Royko</option> 168 <option value="2">Bob Woodward</option> 169 </select></li> 170 <li>Categories: <select multiple="multiple" name="categories"> 171 <option value="1">Entertainment</option> 172 <option value="2">It's a test</option> 173 <option value="3">Third test</option> 174 </select></li> 175 >>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 'writer': u'1'}) 176 >>> f.is_valid() 177 True 178 >>> new_art = f.apply_changes() 179 >>> new_art.id 180 1 181 >>> new_art = Article.objects.get(id=1) 182 >>> new_art.headline 183 'New headline' 153 184 """}
