Ticket #6302: pass_initial_through_with_tests.diff
File pass_initial_through_with_tests.diff, 5.7 KB (added by , 17 years ago) |
---|
-
django/db/models/fields/__init__.py
13 13 from django.core import validators 14 14 from django import oldforms 15 15 from django import newforms as forms 16 from django.newforms.util import InitialData 16 17 from django.core.exceptions import ObjectDoesNotExist 17 18 from django.utils.functional import curry 18 19 from django.utils.itercompat import tee … … 797 798 return os.path.normpath(f) 798 799 799 800 def save_form_data(self, instance, data): 800 if data :801 if data and not isinstance(data, InitialData): 801 802 getattr(instance, "save_%s_file" % self.name)(data.filename, data.content, save=False) 802 803 803 804 def formfield(self, **kwargs): -
django/newforms/util.py
67 67 # AttributeError: ValidationError instance has no attribute 'args' 68 68 # See http://www.python.org/doc/current/tut/node10.html#handling 69 69 return repr(self.messages) 70 71 class InitialData(object): 72 73 value = None 74 75 def __init__(self, value): 76 self.value = value -
django/newforms/fields.py
19 19 from django.utils.translation import ugettext_lazy as _ 20 20 from django.utils.encoding import StrAndUnicode, smart_unicode, smart_str 21 21 22 from util import ErrorList, ValidationError 22 from util import ErrorList, ValidationError, InitialData 23 23 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput 24 24 25 25 … … 441 441 super(FileField, self).clean(data) 442 442 if not self.required and data in EMPTY_VALUES: 443 443 return None 444 elif self.required and isinstance(data, InitialData): 445 return data 444 446 try: 445 447 f = UploadedFile(data['filename'], data['content']) 446 448 except TypeError: -
django/newforms/forms.py
9 9 from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode 10 10 from django.utils.safestring import mark_safe 11 11 12 from fields import Field 12 from fields import Field, EMPTY_VALUES 13 13 from widgets import TextInput, Textarea 14 14 from util import flatatt, ErrorDict, ErrorList, ValidationError 15 15 … … 181 181 # Each widget type knows how to retrieve its own data, because some 182 182 # widgets split data over several HTML fields. 183 183 value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) 184 if value in EMPTY_VALUES: 185 value = field.widget.value_from_initial(self.initial, name, value) 184 186 try: 185 187 value = field.clean(value) 186 188 self.cleaned_data[name] = value -
django/newforms/widgets.py
15 15 from django.utils.translation import ugettext 16 16 from django.utils.encoding import StrAndUnicode, force_unicode 17 17 from django.utils.safestring import mark_safe 18 from util import flatatt 18 from util import flatatt, InitialData 19 19 20 20 __all__ = ( 21 21 'Widget', 'TextInput', 'PasswordInput', … … 77 77 return id_ 78 78 id_for_label = classmethod(id_for_label) 79 79 80 def value_from_initial(self, initial, name, data): 81 return data 82 80 83 class Input(Widget): 81 84 """ 82 85 Base class for all <input> widgets (except type='checkbox' and … … 143 146 "File widgets take data from FILES, not POST" 144 147 return files.get(name, None) 145 148 149 def value_from_initial(self, initial, name, data): 150 if initial.has_key(name): 151 return InitialData(initial.get(name)) 152 else: 153 return data 154 146 155 class Textarea(Widget): 147 156 def __init__(self, attrs=None): 148 157 # The 'rows' and 'cols' attributes are required for HTML correctness. -
tests/modeltests/model_forms/models.py
55 55 def __unicode__(self): 56 56 return self.phone 57 57 58 class TextFile(models.Model): 59 file = models.FileField(upload_to='textfiles') 60 description = models.CharField(max_length=20) 61 62 def __unicode__(self): 63 return self.description 64 58 65 __test__ = {'API_TESTS': """ 59 66 >>> from django import newforms as forms 60 67 >>> from django.newforms.models import ModelForm … … 701 708 True 702 709 >>> f.cleaned_data 703 710 {'phone': u'312-555-1212', 'description': u'Assistance'} 711 712 >>> class TextFileForm(ModelForm): 713 ... class Meta: 714 ... model = TextFile 715 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename':'test.txt', 'content': 'testtext'}}) 716 >>> f.is_valid() 717 True 718 >>> f.cleaned_data['file'].__class__.__name__ 719 'UploadedFile' 720 >>> instance = f.save() 721 >>> print instance.file 722 textfiles/test.txt 723 >>> f = TextFileForm(data={'description': u'Assistance'}, instance=instance) 724 >>> f.is_valid() 725 True 726 >>> instance = f.save() 727 >>> print instance.file 728 textfiles/test.txt 729 >>> instance.delete() 704 730 """}