Ticket #9223: 9223.4.diff
File 9223.4.diff, 5.6 KB (added by , 15 years ago) |
---|
-
django/forms/models.py
=== modified file 'django/forms/models.py'
159 159 data[f.name] = f.value_from_object(instance) 160 160 return data 161 161 162 def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda f: f.formfield()):162 def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): 163 163 """ 164 164 Returns a ``SortedDict`` containing form fields for the given model. 165 165 … … 179 179 continue 180 180 if exclude and f.name in exclude: 181 181 continue 182 formfield = formfield_callback(f) 182 if widgets and f.name in widgets: 183 kwargs = {'widget': widgets[f.name]} 184 else: 185 kwargs = {} 186 formfield = formfield_callback(f, **kwargs) 183 187 if formfield: 184 188 field_list.append((f.name, formfield)) 185 189 field_dict = SortedDict(field_list) … … 192 196 self.model = getattr(options, 'model', None) 193 197 self.fields = getattr(options, 'fields', None) 194 198 self.exclude = getattr(options, 'exclude', None) 199 self.widgets = getattr(options, 'widgets', None) 195 200 196 201 197 202 class ModelFormMetaclass(type): 198 203 def __new__(cls, name, bases, attrs): 199 204 formfield_callback = attrs.pop('formfield_callback', 200 lambda f : f.formfield())205 lambda f, **kwargs: f.formfield(**kwargs)) 201 206 try: 202 207 parents = [b for b in bases if issubclass(b, ModelForm)] 203 208 except NameError: … … 215 220 if opts.model: 216 221 # If a model is defined, extract form fields from it. 217 222 fields = fields_for_model(opts.model, opts.fields, 218 opts.exclude, formfield_callback)223 opts.exclude, opts.widgets, formfield_callback) 219 224 # Override default model fields with any custom declared ones 220 225 # (plus, include all the other declared fields). 221 226 fields.update(declared_fields) -
docs/topics/forms/modelforms.txt
=== modified file 'docs/topics/forms/modelforms.txt'
350 350 351 351 .. _section on saving forms: `The save() method`_ 352 352 353 Overriding the default field types 354 ---------------------------------- 353 Overriding the default field types or widgets 354 --------------------------------------------- 355 355 356 356 The default field types, as described in the `Field types`_ table above, are 357 357 sensible defaults. If you have a ``DateField`` in your model, chances are you'd 358 358 want that to be represented as a ``DateField`` in your form. But 359 ``ModelForm`` gives you the flexibility of changing the form field type 360 for a given model field. You do this by declaratively specifying fields like 361 you would in a regular ``Form``. Declared fields will override the default 362 ones generated by using the ``model`` attribute. 359 ``ModelForm`` gives you the flexibility of changing the form field type and 360 widget for a given model field. 361 362 To specify a custom widget for a field use the ``widgets`` attribute og the 363 inner ``Meta`` class. For example if you want the default textarea for the TextField 364 to have custom ``cols`` and ``rows`` attributes you can override its widget: 365 366 >>> class ArticleForm(ModelForm) 367 ... class Meta: 368 ... model = ArticleForm 369 ... fields = ['title', 'text'] 370 ... widgets = { 371 ... 'text': Textarea(attrs={'cols': 80, 'rows': 20}) 372 ... } 373 374 ``Widgets`` dictionary accepts both widget instances (i.e. ``Textarea(...)``) as well 375 as classes (i.e. ``Textarea``). 376 377 If you want to further customize the field including its type, label etc., you do this 378 by declaratively specifying fields like you would in a regular ``Form``. Declared 379 fields will override the default ones generated by using the ``model`` attribute. 363 380 364 381 For example, if you wanted to use ``MyDateFormField`` for the ``pub_date`` 365 382 field, you could do the following:: … … 370 387 ... class Meta: 371 388 ... model = Article 372 389 373 If you want to override a field's default widget, then specify the ``widget``390 If you want to override a field's default label, then specify the ``label`` 374 391 parameter when declaring the form field:: 375 392 376 393 >>> class ArticleForm(ModelForm): 377 ... pub_date = DateField( widget=MyDateWidget())394 ... pub_date = DateField(label='Publication date') 378 395 ... 379 396 ... class Meta: 380 397 ... model = Article -
tests/modeltests/model_forms/models.py
=== modified file 'tests/modeltests/model_forms/models.py'
287 287 >>> CategoryForm.base_fields.keys() 288 288 ['name'] 289 289 290 Using 'widgets' 291 292 >>> class CategoryForm(ModelForm): 293 ... 294 ... class Meta: 295 ... model = Category 296 ... fields = ['name', 'url', 'slug'] 297 ... widgets = { 298 ... 'name': forms.Textarea, 299 ... 'url': forms.TextInput(attrs={'class': 'url'}) 300 ... } 301 302 >>> str(CategoryForm()['name']) 303 '<textarea id="id_name" rows="10" cols="40" name="name"></textarea>' 304 305 >>> str(CategoryForm()['url']) 306 '<input id="id_url" type="text" class="url" name="url" maxlength="40" />' 307 308 >>> str(CategoryForm()['slug']) 309 '<input id="id_slug" type="text" name="slug" maxlength="20" />' 310 290 311 Don't allow more than one 'model' definition in the inheritance hierarchy. 291 312 Technically, it would generate a valid form, but the fact that the resulting 292 313 save method won't deal with multiple objects is likely to trip up people not