Methods

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

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

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

BaseTemporalField

DateField

Validates that the input can be converted to a date. Returns a Python
datetime.date object.

defto_python(self,value):""" Validates that the input can be converted to a date. Returns a Python datetime.date object. """ifvalueinself.empty_values:returnNoneifisinstance(value,datetime.datetime):returnvalue.date()ifisinstance(value,datetime.date):returnvaluereturnsuper(DateField,self).to_python(value)

BaseTemporalField

defto_python(self,value):# Try to coerce the value to unicode.unicode_value=force_text(value,strings_only=True)ifisinstance(unicode_value,six.text_type):value=unicode_value.strip()# If unicode, try to strptime against each input format.ifisinstance(value,six.text_type):forformatinself.input_formats:try:returnself.strptime(value,format)except(ValueError,TypeError):continueraiseValidationError(self.error_messages['invalid'],code='invalid')