Methods

FileField

Field

Return the value that should be shown for this field on render of a
bound form, given the submitted POST data for the field and the initial
data, if any.
For most fields, this will simply be data; FileFields need to handle it
a bit differently.

defbound_data(self,data,initial):""" Return the value that should be shown for this field on render of a bound form, given the submitted POST data for the field and the initial data, if any. For most fields, this will simply be data; FileFields need to handle it a bit differently. """ifself.disabled:returninitialreturndata

FileField

defclean(self,data,initial=None):# If the widget got contradictory inputs, we raise a validation errorifdataisFILE_INPUT_CONTRADICTION:raiseValidationError(self.error_messages['contradiction'],code='contradiction')# False means the field value should be cleared; further validation is# not needed.ifdataisFalse:ifnotself.required:returnFalse# If the field is required, clearing is not possible (the widget# shouldn't return False data in that case anyway). False is not# in self.empty_value; if a False value makes it this far# it should be validated from here on out as None (so it will be# caught by the required check).data=Noneifnotdataandinitial:returninitialreturnsuper(FileField,self).clean(data)

Field

Validates the given value and returns its "cleaned" value as an
appropriate Python object.
Raises ValidationError for any errors.

defclean(self,value):""" Validates the given value and returns its "cleaned" value as an appropriate Python object. Raises ValidationError for any errors. """value=self.to_python(value)self.validate(value)self.run_validators(value)returnvalue

FileField

defhas_changed(self,initial,data):ifdataisNone:returnFalsereturnTrue

Field

Return True if data differs from initial.

defhas_changed(self,initial,data):""" Return True if data differs from initial. """try:data=self.to_python(data)ifhasattr(self,'_coerce'):returnself._coerce(data)!=self._coerce(initial)exceptValidationError:returnTrue# For purposes of seeing whether something has changed, None is# the same as an empty string, if the data or initial value we get# is None, replace it with ''.initial_value=initialifinitialisnotNoneelse''data_value=dataifdataisnotNoneelse''returninitial_value!=data_value

ImageField

defto_python(self,data):""" Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others -- whatever the Python Imaging Library supports). """f=super(ImageField,self).to_python(data)iffisNone:returnNonefromPILimportImage# We need to get a file object for Pillow. We might have a path or we might# have to read the data into memory.ifhasattr(data,'temporary_file_path'):file=data.temporary_file_path()else:ifhasattr(data,'read'):file=BytesIO(data.read())else:file=BytesIO(data['content'])try:# load() could spot a truncated JPEG, but it loads the entire# image in memory, which is a DoS vector. See #3848 and #18520.image=Image.open(file)# verify() must be called immediately after the constructor.image.verify()# Annotating so subclasses can reuse it for their own validationf.image=image# Pillow doesn't detect the MIME type of all formats. In those# cases, content_type will be None.f.content_type=Image.MIME.get(image.format)exceptException:# Pillow doesn't recognize it as an image.six.reraise(ValidationError,ValidationError(self.error_messages['invalid_image'],code='invalid_image',),sys.exc_info()[2])ifhasattr(f,'seek')andcallable(f.seek):f.seek(0)returnf

FileField

defto_python(self,data):ifdatainself.empty_values:returnNone# UploadedFile objects should have name and size attributes.try:file_name=data.namefile_size=data.sizeexceptAttributeError:raiseValidationError(self.error_messages['invalid'],code='invalid')ifself.max_lengthisnotNoneandlen(file_name)>self.max_length:params={'max':self.max_length,'length':len(file_name)}raiseValidationError(self.error_messages['max_length'],code='max_length',params=params)ifnotfile_name:raiseValidationError(self.error_messages['invalid'],code='invalid')ifnotself.allow_empty_fileandnotfile_size:raiseValidationError(self.error_messages['empty'],code='empty')returndata