Ticket #5517: media-for-base-forms.diff

File media-for-base-forms.diff, 2.6 KB (added by Petr Marhoun <petr.marhoun@…>, 12 years ago)
  • django/newforms/forms.py

    === modified file 'django/newforms/forms.py'
     
    99from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
    1010
    1111from fields import Field
    12 from widgets import Media, media_property, TextInput, Textarea
     12from widgets import Media, MediaDefiningClass, TextInput, Textarea
    1313from util import flatatt, ErrorDict, ErrorList, ValidationError
    1414
    1515__all__ = ('BaseForm', 'Form')
     
    3333    def copy(self):
    3434        return SortedDictFromList([(k, copy.deepcopy(v)) for k, v in self.items()])
    3535
    36 class DeclarativeFieldsMetaclass(type):
     36class DeclarativeFieldsMetaclass(MediaDefiningClass):
    3737    """
    3838    Metaclass that converts Field attributes to a dictionary called
    3939    'base_fields', taking into account parent class 'base_fields' as well.
     
    5252
    5353        attrs['base_fields'] = SortedDictFromList(fields)
    5454
    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)
    5956
    6057class BaseForm(StrAndUnicode):
    6158    # This is the main implementation of all the Form logic. Note that this
    6259    # class is different than Form. See the comments by the Form class for more
    6360    # information. Any improvements to the form API should be made to *this*
    6461    # class, not to the Form class.
     62    __metaclass__ = MediaDefiningClass
    6563    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    6664            initial=None, error_class=ErrorList):
    6765        self.is_bound = data is not None or files is not None
  • tests/modeltests/model_forms/models.py

    === modified file 'tests/modeltests/model_forms/models.py'
     
    207207>>> f.say_hello()
    208208hello
    209209
     210You 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
    210221Use form_for_instance to create a Form from a model instance. The difference
    211222between this Form and one created via form_for_model is that the object's
    212223current values are inserted as 'initial' data in each Field.
Back to Top