=== modified file 'django/newforms/forms.py'
|
|
|
9 | 9 | from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode |
10 | 10 | |
11 | 11 | from fields import Field |
12 | | from widgets import Media, media_property, TextInput, Textarea |
| 12 | from widgets import Media, MediaDefiningClass, TextInput, Textarea |
13 | 13 | from util import flatatt, ErrorDict, ErrorList, ValidationError |
14 | 14 | |
15 | 15 | __all__ = ('BaseForm', 'Form') |
… |
… |
|
33 | 33 | def copy(self): |
34 | 34 | return SortedDictFromList([(k, copy.deepcopy(v)) for k, v in self.items()]) |
35 | 35 | |
36 | | class DeclarativeFieldsMetaclass(type): |
| 36 | class DeclarativeFieldsMetaclass(MediaDefiningClass): |
37 | 37 | """ |
38 | 38 | Metaclass that converts Field attributes to a dictionary called |
39 | 39 | 'base_fields', taking into account parent class 'base_fields' as well. |
… |
… |
|
52 | 52 | |
53 | 53 | attrs['base_fields'] = SortedDictFromList(fields) |
54 | 54 | |
55 | | new_class = type.__new__(cls, name, bases, attrs) |
56 | | if 'media' not in attrs: |
57 | | new_class.media = media_property(new_class) |
58 | | return new_class |
| 55 | return MediaDefiningClass.__new__(cls, name, bases, attrs) |
59 | 56 | |
60 | 57 | class BaseForm(StrAndUnicode): |
61 | 58 | # This is the main implementation of all the Form logic. Note that this |
62 | 59 | # class is different than Form. See the comments by the Form class for more |
63 | 60 | # information. Any improvements to the form API should be made to *this* |
64 | 61 | # class, not to the Form class. |
| 62 | __metaclass__ = MediaDefiningClass |
65 | 63 | def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, |
66 | 64 | initial=None, error_class=ErrorList): |
67 | 65 | self.is_bound = data is not None or files is not None |
=== modified file 'tests/modeltests/model_forms/models.py'
|
|
|
207 | 207 | >>> f.say_hello() |
208 | 208 | hello |
209 | 209 | |
| 210 | You can define media class on base form. |
| 211 | >>> class FormWithMedia(BaseForm): |
| 212 | ... class Media: |
| 213 | ... js = ['form.js'] |
| 214 | ... css = {'all': ['form.css']} |
| 215 | >>> CategoryFormWithMedia = form_for_model(Category, form=FormWithMedia) |
| 216 | >>> f = CategoryFormWithMedia() |
| 217 | >>> print f.media |
| 218 | <link href="form.css" type="text/css" media="all" rel="stylesheet" /> |
| 219 | <script type="text/javascript" src="form.js"></script> |
| 220 | |
210 | 221 | Use form_for_instance to create a Form from a model instance. The difference |
211 | 222 | between this Form and one created via form_for_model is that the object's |
212 | 223 | current values are inserted as 'initial' data in each Field. |