Ticket #14039: 14039_1.4.3_with_note.diff

File 14039_1.4.3_with_note.diff, 4.5 KB (added by bradleyayers, 3 years ago)

Updated to apply cleanly to 1.4.3

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 527a3c0..c7cb731 100644
    a b class Field(object): 
    6666                    u'already exists.'),
    6767    }
    6868
     69    defer_save = False  # If true, defer saving until after other fields.
     70
    6971    # Generic field type description, usually overriden by subclasses
    7072    def _description(self):
    7173        return _(u'Field of type: %(field_type)s') % {
  • django/db/models/fields/files.py

    diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
    index 6f1f183..ae4d526 100644
    a b class FileDescriptor(object): 
    203203    def __set__(self, instance, value):
    204204        instance.__dict__[self.field.name] = value
    205205
     206
    206207class FileField(Field):
    207208    # The class to wrap instance attributes in. Accessing the file object off
    208209    # the instance will always return an instance of attr_class.
    class FileField(Field): 
    211212    # The descriptor to use for accessing the attribute off of the class.
    212213    descriptor_class = FileDescriptor
    213214
     215    defer_save = True
     216
    214217    description = _("File")
    215218
    216219    def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, **kwargs):
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 16007e5..21ac628 100644
    a b class Field(object): 
    5151        'invalid': _(u'Enter a valid value.'),
    5252    }
    5353
     54    # If true, clean() takes initial value as well as data.
     55    clean_takes_initial = False
     56
    5457    # Tracks each time a Field instance is created. Used to retain order.
    5558    creation_counter = 0
    5659
    class FileField(Field): 
    489492        'contradiction': _(u'Please either submit a file or check the clear checkbox, not both.')
    490493    }
    491494
     495    clean_takes_initial = True
     496
    492497    def __init__(self, *args, **kwargs):
    493498        self.max_length = kwargs.pop('max_length', None)
    494499        self.allow_empty_file = kwargs.pop('allow_empty_file', False)
    class MultiValueField(Field): 
    843848    "compressed" version of those values -- a single value.
    844849
    845850    You'll probably want to use this with MultiWidget.
     851
     852    If your MultiValueField subclass includes a FileField or derivative as one
     853    of its fields, there will be subtle differences in behavior (particularly
     854    displaying a bound form with initial data) unless you set your subclasses'
     855    ``clean_takes_initial`` attribute to True and override the clean() method
     856    to accept an ``initial`` arg and pass it (or an appropriate part of it) on
     857    to the member FileField.
    846858    """
    847859    default_error_messages = {
    848860        'invalid': _(u'Enter a list of values.'),
  • django/forms/forms.py

    diff --git a/django/forms/forms.py b/django/forms/forms.py
    index 94eb22d..61d11d1 100644
    a b class BaseForm(StrAndUnicode): 
    280280            # widgets split data over several HTML fields.
    281281            value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
    282282            try:
    283                 if isinstance(field, FileField):
     283                if field.clean_takes_initial:
    284284                    initial = self.initial.get(name, field.initial)
    285285                    value = field.clean(value, initial)
    286286                else:
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index cd8f027..d37d393 100644
    a b def construct_instance(form, instance, fields=None, exclude=None): 
    3434    opts = instance._meta
    3535
    3636    cleaned_data = form.cleaned_data
    37     file_field_list = []
     37    deferred_list = []
    3838    for f in opts.fields:
    3939        if not f.editable or isinstance(f, models.AutoField) \
    4040                or not f.name in cleaned_data:
    def construct_instance(form, instance, fields=None, exclude=None): 
    4545            continue
    4646        # Defer saving file-type fields until after the other fields, so a
    4747        # callable upload_to can use the values from other fields.
    48         if isinstance(f, models.FileField):
    49             file_field_list.append(f)
     48        if f.defer_save:
     49            deferred_list.append(f)
    5050        else:
    5151            f.save_form_data(instance, cleaned_data[f.name])
    5252
    53     for f in file_field_list:
     53    for f in deferred_list:
    5454        f.save_form_data(instance, cleaned_data[f.name])
    5555
    5656    return instance
Back to Top