6 from itertools
import tee
8 import django
.utils
.copycompat
as copy
10 from django
.db
import connection
11 from django
.db
.models
.fields
.subclassing
import LegacyConnection
12 from django
.db
.models
.query_utils
import QueryWrapper
13 from django
.conf
import settings
14 from django
import forms
15 from django
.core
import exceptions
, validators
16 from django
.utils
.datastructures
import DictWrapper
17 from django
.utils
.functional
import curry
18 from django
.utils
.text
import capfirst
19 from django
.utils
.translation
import ugettext_lazy
as _
20 from django
.utils
.encoding
import smart_unicode
, force_unicode
, smart_str
21 from django
.utils
import datetime_safe
26 # The values to use for "blank" in SelectFields. Will be appended to the start of most "choices" lists.
27 BLANK_CHOICE_DASH
= [("", "---------")]
28 BLANK_CHOICE_NONE
= [("", "None")]
30 class FieldDoesNotExist(Exception):
33 # A guide to Field parameters:
35 # * name: The name of the field specifed in the model.
36 # * attname: The attribute to use on the model object. This is the same as
37 # "name", except in the case of ForeignKeys, where "_id" is
39 # * db_column: The db_column specified in the model (or None).
40 # * column: The database column for this field. This is the same as
41 # "attname", except if db_column is specified.
43 # Code that introspects values, or does other dynamic things, should use
44 # attname. For example, this gets the primary key value of object "obj":
46 # getattr(obj, opts.pk.attname)
49 """Base class for all field types"""
50 __metaclass__
= LegacyConnection
52 # Designates whether empty strings fundamentally are allowed at the
54 empty_strings_allowed
= True
56 # These track each time a Field instance is created. Used to retain order.
57 # The auto_creation_counter is used for fields that Django implicitly
58 # creates, creation_counter is used for all user-specified fields.
60 auto_creation_counter
= -1
61 default_validators
= [] # Default set of validators
62 default_error_messages
= {
63 'invalid_choice': _(u
'Value %r is not a valid choice.'),
64 'null': _(u
'This field cannot be null.'),
65 'blank': _(u
'This field cannot be blank.'),
68 # Generic field type description, usually overriden by subclasses
69 def _description(self
):
70 return _(u
'Field of type: %(field_type)s') % {
71 'field_type': self
.__class
__.__name
__
73 description
= property(_description
)
75 def __init__(self
, verbose_name
=None, name
=None, primary_key
=False,
76 max_length
=None, unique
=False, blank
=False, null
=False,
77 db_index
=False, rel
=None, default
=NOT_PROVIDED
, editable
=True,
78 serialize
=True, unique_for_date
=None, unique_for_month
=None,
79 unique_for_year
=None, choices
=None, help_text
='', db_column
=None,
80 db_tablespace
=None, auto_created
=False, validators
=[],
83 self
.verbose_name
= verbose_name
84 self
.primary_key
= primary_key
85 self
.max_length
, self
._unique
= max_length
, unique
86 self
.blank
, self
.null
= blank
, null
87 # Oracle treats the empty string ('') as null, so coerce the null
88 # option whenever '' is a possible value.
89 if self
.empty_strings_allowed
and connection
.features
.interprets_empty_strings_as_nulls
:
92 self
.default
= default
93 self
.editable
= editable
94 self
.serialize
= serialize
95 self
.unique_for_date
, self
.unique_for_month
= unique_for_date
, unique_for_month
96 self
.unique_for_year
= unique_for_year
97 self
._choices
= choices
or []
98 self
.help_text
= help_text
99 self
.db_column
= db_column
100 self
.db_tablespace
= db_tablespace
or settings
.DEFAULT_INDEX_TABLESPACE
101 self
.auto_created
= auto_created
103 # Set db_index to True if the field has a relationship and doesn't explicitly set db_index.
104 self
.db_index
= db_index
106 # Adjust the appropriate creation counter, and save our local copy.
108 self
.creation_counter
= Field
.auto_creation_counter
109 Field
.auto_creation_counter
-= 1
111 self
.creation_counter
= Field
.creation_counter
112 Field
.creation_counter
+= 1
114 self
.validators
= self
.default_validators
+ validators
117 for c
in reversed(self
.__class
__.__mro
__):
118 messages
.update(getattr(c
, 'default_error_messages', {}))
119 messages
.update(error_messages
or {})
120 self
.error_messages
= messages
122 def __cmp__(self
, other
):
123 # This is needed because bisect does not take a comparison function.
124 return cmp(self
.creation_counter
, other
.creation_counter
)
126 def __deepcopy__(self
, memodict
):
127 # We don't have to deepcopy very much here, since most things are not
128 # intended to be altered after initial creation.
129 obj
= copy
.copy(self
)
131 obj
.rel
= copy
.copy(self
.rel
)
132 memodict
[id(self
)] = obj
135 def to_python(self
, value
):
137 Converts the input value into the expected Python data type, raising
138 django.core.exceptions.ValidationError if the data can't be converted.
139 Returns the converted value. Subclasses should override this.
143 def run_validators(self
, value
):
144 if value
in validators
.EMPTY_VALUES
:
148 for v
in self
.validators
:
151 except exceptions
.ValidationError
, e
:
152 if hasattr(e
, 'code') and e
.code
in self
.error_messages
:
153 message
= self
.error_messages
[e
.code
]
155 message
= message
% e
.params
156 errors
.append(message
)
158 errors
.extend(e
.messages
)
160 raise exceptions
.ValidationError(errors
)
162 def validate(self
, value
, model_instance
):
164 Validates value and throws ValidationError. Subclasses should override
165 this to provide validation logic.
167 if not self
.editable
:
168 # Skip validation for non-editable fields.
170 if self
._choices
and value
:
171 for option_key
, option_value
in self
.choices
:
172 if isinstance(option_value
, (list, tuple)):
173 # This is an optgroup, so look inside the group for options.
174 for optgroup_key
, optgroup_value
in option_value
:
175 if value
== optgroup_key
:
177 elif value
== option_key
:
179 raise exceptions
.ValidationError(self
.error_messages
['invalid_choice'] % value
)
181 if value
is None and not self
.null
:
182 raise exceptions
.ValidationError(self
.error_messages
['null'])
184 if not self
.blank
and value
in validators
.EMPTY_VALUES
:
185 raise exceptions
.ValidationError(self
.error_messages
['blank'])
187 def clean(self
, value
, model_instance
):
189 Convert the value's type and run validation. Validation errors from to_python
190 and validate are propagated. The correct value is returned if no error is
193 value
= self
.to_python(value
)
194 self
.validate(value
, model_instance
)
195 self
.run_validators(value
)
198 def db_type(self
, connection
):
200 Returns the database column data type for this field, for the provided
203 # The default implementation of this method looks at the
204 # backend-specific DATA_TYPES dictionary, looking up the field by its
207 # A Field class can implement the get_internal_type() method to specify
208 # which *preexisting* Django Field class it's most similar to -- i.e.,
209 # an XMLField is represented by a TEXT column type, which is the same
210 # as the TextField Django field type, which means XMLField's
211 # get_internal_type() returns 'TextField'.
213 # But the limitation of the get_internal_type() / data_types approach
214 # is that it cannot handle database column types that aren't already
215 # mapped to one of the built-in Django field types. In this case, you
216 # can implement db_type() instead of get_internal_type() to specify
217 # exactly which wacky database column type you want to use.
218 data
= DictWrapper(self
.__dict
__, connection
.ops
.quote_name
, "qn_")
220 return connection
.creation
.data_types
[self
.get_internal_type()] % data
225 return self
._unique
or self
.primary_key
226 unique
= property(unique
)
228 def set_attributes_from_name(self
, name
):
230 self
.attname
, self
.column
= self
.get_attname_column()
231 if self
.verbose_name
is None and name
:
232 self
.verbose_name
= name
.replace('_', ' ')
234 def contribute_to_class(self
, cls
, name
):
235 self
.set_attributes_from_name(name
)
237 cls
._meta
.add_field(self
)
239 setattr(cls
, 'get_%s_display' % self
.name
, curry(cls
._get
_FIELD
_display
, field
=self
))
241 def get_attname(self
):
244 def get_attname_column(self
):
245 attname
= self
.get_attname()
246 column
= self
.db_column
or attname
247 return attname
, column
249 def get_cache_name(self
):
250 return '_%s_cache' % self
.name
252 def get_internal_type(self
):
253 return self
.__class
__.__name
__
255 def pre_save(self
, model_instance
, add
):
256 "Returns field's value just before saving."
257 return getattr(model_instance
, self
.attname
)
259 def get_prep_value(self
, value
):
260 "Perform preliminary non-db specific value checks and conversions."
263 def get_db_prep_value(self
, value
, connection
, prepared
=False):
264 """Returns field's value prepared for interacting with the database
267 Used by the default implementations of ``get_db_prep_save``and
268 `get_db_prep_lookup```
271 value
= self
.get_prep_value(value
)
274 def get_db_prep_save(self
, value
, connection
):
275 "Returns field's value prepared for saving into a database."
276 return self
.get_db_prep_value(value
, connection
=connection
, prepared
=False)
278 def get_prep_lookup(self
, lookup_type
, value
):
279 "Perform preliminary non-db specific lookup checks and conversions"
280 if hasattr(value
, 'prepare'):
281 return value
.prepare()
282 if hasattr(value
, '_prepare'):
283 return value
._prepare
()
286 'regex', 'iregex', 'month', 'day', 'week_day', 'search',
287 'contains', 'icontains', 'iexact', 'startswith', 'istartswith',
288 'endswith', 'iendswith', 'isnull'
291 elif lookup_type
in ('exact', 'gt', 'gte', 'lt', 'lte'):
292 return self
.get_prep_value(value
)
293 elif lookup_type
in ('range', 'in'):
294 return [self
.get_prep_value(v
) for v
in value
]
295 elif lookup_type
== 'year':
299 raise ValueError("The __year lookup type requires an integer argument")
301 raise TypeError("Field has invalid lookup: %s" % lookup_type
)
303 def get_db_prep_lookup(self
, lookup_type
, value
, connection
, prepared
=False):
304 "Returns field's value prepared for database lookup."
306 value
= self
.get_prep_lookup(lookup_type
, value
)
307 if hasattr(value
, 'get_compiler'):
308 value
= value
.get_compiler(connection
=connection
)
309 if hasattr(value
, 'as_sql') or hasattr(value
, '_as_sql'):
310 # If the value has a relabel_aliases method, it will need to
311 # be invoked before the final SQL is evaluated
312 if hasattr(value
, 'relabel_aliases'):
314 if hasattr(value
, 'as_sql'):
315 sql
, params
= value
.as_sql()
317 sql
, params
= value
._as
_sql
(connection
=connection
)
318 return QueryWrapper(('(%s)' % sql
), params
)
320 if lookup_type
in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'):
322 elif lookup_type
in ('exact', 'gt', 'gte', 'lt', 'lte'):
323 return [self
.get_db_prep_value(value
, connection
=connection
, prepared
=prepared
)]
324 elif lookup_type
in ('range', 'in'):
325 return [self
.get_db_prep_value(v
, connection
=connection
, prepared
=prepared
) for v
in value
]
326 elif lookup_type
in ('contains', 'icontains'):
327 return ["%%%s%%" % connection
.ops
.prep_for_like_query(value
)]
328 elif lookup_type
== 'iexact':
329 return [connection
.ops
.prep_for_iexact_query(value
)]
330 elif lookup_type
in ('startswith', 'istartswith'):
331 return ["%s%%" % connection
.ops
.prep_for_like_query(value
)]
332 elif lookup_type
in ('endswith', 'iendswith'):
333 return ["%%%s" % connection
.ops
.prep_for_like_query(value
)]
334 elif lookup_type
== 'isnull':
336 elif lookup_type
== 'year':
337 if self
.get_internal_type() == 'DateField':
338 return connection
.ops
.year_lookup_bounds_for_date_field(value
)
340 return connection
.ops
.year_lookup_bounds(value
)
342 def has_default(self
):
343 "Returns a boolean of whether this field has a default value."
344 return self
.default
is not NOT_PROVIDED
346 def get_default(self
):
347 "Returns the default value for this field."
348 if self
.has_default():
349 if callable(self
.default
):
350 return self
.default()
351 return force_unicode(self
.default
, strings_only
=True)
352 if not self
.empty_strings_allowed
or (self
.null
and not connection
.features
.interprets_empty_strings_as_nulls
):
356 def get_validator_unique_lookup_type(self
):
357 return '%s__exact' % self
.name
359 def get_choices(self
, include_blank
=True, blank_choice
=BLANK_CHOICE_DASH
):
360 """Returns choices with a default blank choices included, for use
361 as SelectField choices for this field."""
362 first_choice
= include_blank
and blank_choice
or []
364 return first_choice
+ list(self
.choices
)
365 rel_model
= self
.rel
.to
366 if hasattr(self
.rel
, 'get_related_field'):
367 lst
= [(getattr(x
, self
.rel
.get_related_field().attname
), smart_unicode(x
)) for x
in rel_model
._default
_manager
.complex_filter(self
.rel
.limit_choices_to
)]
369 lst
= [(x
._get
_pk
_val
(), smart_unicode(x
)) for x
in rel_model
._default
_manager
.complex_filter(self
.rel
.limit_choices_to
)]
370 return first_choice
+ lst
372 def get_choices_default(self
):
373 return self
.get_choices()
375 def get_flatchoices(self
, include_blank
=True, blank_choice
=BLANK_CHOICE_DASH
):
376 "Returns flattened choices with a default blank choice included."
377 first_choice
= include_blank
and blank_choice
or []
378 return first_choice
+ list(self
.flatchoices
)
380 def _get_val_from_obj(self
, obj
):
382 return getattr(obj
, self
.attname
)
384 return self
.get_default()
386 def value_to_string(self
, obj
):
388 Returns a string value of this field from the passed obj.
389 This is used by the serialization framework.
391 return smart_unicode(self
._get
_val
_from
_obj
(obj
))
393 def bind(self
, fieldmapping
, original
, bound_field_class
):
394 return bound_field_class(self
, fieldmapping
, original
)
396 def _get_choices(self
):
397 if hasattr(self
._choices
, 'next'):
398 choices
, self
._choices
= tee(self
._choices
)
402 choices
= property(_get_choices
)
404 def _get_flatchoices(self
):
405 """Flattened version of choices tuple."""
407 for choice
, value
in self
.choices
:
408 if isinstance(value
, (list, tuple)):
411 flat
.append((choice
,value
))
413 flatchoices
= property(_get_flatchoices
)
415 def save_form_data(self
, instance
, data
):
416 setattr(instance
, self
.name
, data
)
418 def formfield(self
, form_class
=forms
.CharField
, **kwargs
):
419 "Returns a django.forms.Field instance for this database Field."
420 defaults
= {'required': not self
.blank
, 'label': capfirst(self
.verbose_name
), 'help_text': self
.help_text
}
421 if self
.has_default():
422 if callable(self
.default
):
423 defaults
['initial'] = self
.default
424 defaults
['show_hidden_initial'] = True
426 defaults
['initial'] = self
.get_default()
428 # Fields with choices get special treatment.
429 include_blank
= self
.blank
or not (self
.has_default() or 'initial' in kwargs
)
430 defaults
['choices'] = self
.get_choices(include_blank
=include_blank
)
431 defaults
['coerce'] = self
.to_python
433 defaults
['empty_value'] = None
434 form_class
= forms
.TypedChoiceField
435 # Many of the subclass-specific formfield arguments (min_value,
436 # max_value) don't apply for choice fields, so be sure to only pass
437 # the values that TypedChoiceField will understand.
438 for k
in kwargs
.keys():
439 if k
not in ('coerce', 'empty_value', 'choices', 'required',
440 'widget', 'label', 'initial', 'help_text',
441 'error_messages', 'show_hidden_initial'):
443 defaults
.update(kwargs
)
444 return form_class(**defaults
)
446 def value_from_object(self
, obj
):
447 "Returns the value of this field in the given model instance."
448 return getattr(obj
, self
.attname
)
450 class AutoField(Field
):
451 description
= _("Integer")
453 empty_strings_allowed
= False
454 default_error_messages
= {
455 'invalid': _(u
'This value must be an integer.'),
457 def __init__(self
, *args
, **kwargs
):
458 assert kwargs
.get('primary_key', False) is True, "%ss must have primary_key=True." % self
.__class
__.__name
__
459 kwargs
['blank'] = True
460 Field
.__init
__(self
, *args
, **kwargs
)
462 def to_python(self
, value
):
467 except (TypeError, ValueError):
468 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
470 def validate(self
, value
, model_instance
):
473 def get_prep_value(self
, value
):
478 def contribute_to_class(self
, cls
, name
):
479 assert not cls
._meta
.has_auto_field
, "A model can't have more than one AutoField."
480 super(AutoField
, self
).contribute_to_class(cls
, name
)
481 cls
._meta
.has_auto_field
= True
482 cls
._meta
.auto_field
= self
484 def formfield(self
, **kwargs
):
487 class BooleanField(Field
):
488 empty_strings_allowed
= False
489 default_error_messages
= {
490 'invalid': _(u
'This value must be either True or False.'),
492 description
= _("Boolean (Either True or False)")
493 def __init__(self
, *args
, **kwargs
):
494 kwargs
['blank'] = True
495 if 'default' not in kwargs
and not kwargs
.get('null'):
496 kwargs
['default'] = False
497 Field
.__init
__(self
, *args
, **kwargs
)
499 def get_internal_type(self
):
500 return "BooleanField"
502 def to_python(self
, value
):
503 if value
in (True, False):
504 # if value is 1 or 0 than it's equal to True or False, but we want
505 # to return a true bool for semantic reasons.
507 if value
in ('t', 'True', '1'):
509 if value
in ('f', 'False', '0'):
511 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
513 def get_prep_lookup(self
, lookup_type
, value
):
514 # Special-case handling for filters coming from a web request (e.g. the
515 # admin interface). Only works for scalar values (not lists). If you're
516 # passing in a list, you might as well make things the right type when
517 # constructing the list.
518 if value
in ('1', '0'):
519 value
= bool(int(value
))
520 return super(BooleanField
, self
).get_prep_lookup(lookup_type
, value
)
522 def get_prep_value(self
, value
):
527 def formfield(self
, **kwargs
):
528 # Unlike most fields, BooleanField figures out include_blank from
529 # self.null instead of self.blank.
531 include_blank
= self
.null
or not (self
.has_default() or 'initial' in kwargs
)
532 defaults
= {'choices': self
.get_choices(include_blank
=include_blank
)}
534 defaults
= {'form_class': forms
.BooleanField
}
535 defaults
.update(kwargs
)
536 return super(BooleanField
, self
).formfield(**defaults
)
538 class CharField(Field
):
539 description
= _("String (up to %(max_length)s)")
541 def __init__(self
, *args
, **kwargs
):
542 super(CharField
, self
).__init
__(*args
, **kwargs
)
543 self
.validators
.append(validators
.MaxLengthValidator(self
.max_length
))
545 def get_internal_type(self
):
548 def to_python(self
, value
):
549 if isinstance(value
, basestring
) or value
is None:
551 return smart_unicode(value
)
553 def get_prep_value(self
, value
):
554 return self
.to_python(value
)
556 def formfield(self
, **kwargs
):
557 # Passing max_length to forms.CharField means that the value's length
558 # will be validated twice. This is considered acceptable since we want
559 # the value in the form field (to pass into widget for example).
560 defaults
= {'max_length': self
.max_length
}
561 defaults
.update(kwargs
)
562 return super(CharField
, self
).formfield(**defaults
)
564 # TODO: Maybe move this into contrib, because it's specialized.
565 class CommaSeparatedIntegerField(CharField
):
566 default_validators
= [validators
.validate_comma_separated_integer_list
]
567 description
= _("Comma-separated integers")
569 def formfield(self
, **kwargs
):
572 'invalid': _(u
'Enter only digits separated by commas.'),
575 defaults
.update(kwargs
)
576 return super(CommaSeparatedIntegerField
, self
).formfield(**defaults
)
578 ansi_date_re
= re
.compile(r
'^\d{4}-\d{1,2}-\d{1,2}$')
580 class DateField(Field
):
581 description
= _("Date (without time)")
583 empty_strings_allowed
= False
584 default_error_messages
= {
585 'invalid': _('Enter a valid date in YYYY-MM-DD format.'),
586 'invalid_date': _('Invalid date: %s'),
588 def __init__(self
, verbose_name
=None, name
=None, auto_now
=False, auto_now_add
=False, **kwargs
):
589 self
.auto_now
, self
.auto_now_add
= auto_now
, auto_now_add
590 #HACKs : auto_now_add/auto_now should be done as a default or a pre_save.
591 if auto_now
or auto_now_add
:
592 kwargs
['editable'] = False
593 kwargs
['blank'] = True
594 Field
.__init
__(self
, verbose_name
, name
, **kwargs
)
596 def get_internal_type(self
):
599 def to_python(self
, value
):
602 if isinstance(value
, datetime
.datetime
):
604 if isinstance(value
, datetime
.date
):
607 if not ansi_date_re
.search(value
):
608 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
609 # Now that we have the date string in YYYY-MM-DD format, check to make
610 # sure it's a valid date.
611 # We could use time.strptime here and catch errors, but datetime.date
612 # produces much friendlier error messages.
613 year
, month
, day
= map(int, value
.split('-'))
615 return datetime
.date(year
, month
, day
)
616 except ValueError, e
:
617 msg
= self
.error_messages
['invalid_date'] % _(str(e
))
618 raise exceptions
.ValidationError(msg
)
620 def pre_save(self
, model_instance
, add
):
621 if self
.auto_now
or (self
.auto_now_add
and add
):
622 value
= datetime
.datetime
.now()
623 setattr(model_instance
, self
.attname
, value
)
626 return super(DateField
, self
).pre_save(model_instance
, add
)
628 def contribute_to_class(self
, cls
, name
):
629 super(DateField
,self
).contribute_to_class(cls
, name
)
631 setattr(cls
, 'get_next_by_%s' % self
.name
,
632 curry(cls
._get
_next
_or
_previous
_by
_FIELD
, field
=self
, is_next
=True))
633 setattr(cls
, 'get_previous_by_%s' % self
.name
,
634 curry(cls
._get
_next
_or
_previous
_by
_FIELD
, field
=self
, is_next
=False))
636 def get_prep_lookup(self
, lookup_type
, value
):
637 # For "__month", "__day", and "__week_day" lookups, convert the value
638 # to an int so the database backend always sees a consistent type.
639 if lookup_type
in ('month', 'day', 'week_day'):
641 return super(DateField
, self
).get_prep_lookup(lookup_type
, value
)
643 def get_prep_value(self
, value
):
644 return self
.to_python(value
)
646 def get_db_prep_value(self
, value
, connection
, prepared
=False):
647 # Casts dates into the format expected by the backend
649 value
= self
.get_prep_value(value
)
650 return connection
.ops
.value_to_db_date(value
)
652 def value_to_string(self
, obj
):
653 val
= self
._get
_val
_from
_obj
(obj
)
657 data
= datetime_safe
.new_date(val
).strftime("%Y-%m-%d")
660 def formfield(self
, **kwargs
):
661 defaults
= {'form_class': forms
.DateField
}
662 defaults
.update(kwargs
)
663 return super(DateField
, self
).formfield(**defaults
)
665 class DateTimeField(DateField
):
666 default_error_messages
= {
667 'invalid': _(u
'Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.'),
669 description
= _("Date (with time)")
671 def get_internal_type(self
):
672 return "DateTimeField"
674 def to_python(self
, value
):
677 if isinstance(value
, datetime
.datetime
):
679 if isinstance(value
, datetime
.date
):
680 return datetime
.datetime(value
.year
, value
.month
, value
.day
)
682 # Attempt to parse a datetime:
683 value
= smart_str(value
)
684 # split usecs, because they are not recognized by strptime.
687 value
, usecs
= value
.split('.')
690 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
693 kwargs
= {'microsecond': usecs
}
694 try: # Seconds are optional, so try converting seconds first.
695 return datetime
.datetime(*time
.strptime(value
, '%Y-%m-%d %H:%M:%S')[:6],
699 try: # Try without seconds.
700 return datetime
.datetime(*time
.strptime(value
, '%Y-%m-%d %H:%M')[:5],
702 except ValueError: # Try without hour/minutes/seconds.
704 return datetime
.datetime(*time
.strptime(value
, '%Y-%m-%d')[:3],
707 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
709 def get_prep_value(self
, value
):
710 return self
.to_python(value
)
712 def get_db_prep_value(self
, value
, connection
, prepared
=False):
713 # Casts dates into the format expected by the backend
715 value
= self
.get_prep_value(value
)
716 return connection
.ops
.value_to_db_datetime(value
)
718 def value_to_string(self
, obj
):
719 val
= self
._get
_val
_from
_obj
(obj
)
723 d
= datetime_safe
.new_datetime(val
)
724 data
= d
.strftime('%Y-%m-%d %H:%M:%S')
727 def formfield(self
, **kwargs
):
728 defaults
= {'form_class': forms
.DateTimeField
}
729 defaults
.update(kwargs
)
730 return super(DateTimeField
, self
).formfield(**defaults
)
732 class DecimalField(Field
):
733 empty_strings_allowed
= False
734 default_error_messages
= {
735 'invalid': _(u
'This value must be a decimal number.'),
737 description
= _("Decimal number")
739 def __init__(self
, verbose_name
=None, name
=None, max_digits
=None, decimal_places
=None, **kwargs
):
740 self
.max_digits
, self
.decimal_places
= max_digits
, decimal_places
741 Field
.__init
__(self
, verbose_name
, name
, **kwargs
)
743 def get_internal_type(self
):
744 return "DecimalField"
746 def to_python(self
, value
):
750 return decimal
.Decimal(value
)
751 except decimal
.InvalidOperation
:
752 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
754 def _format(self
, value
):
755 if isinstance(value
, basestring
) or value
is None:
758 return self
.format_number(value
)
760 def format_number(self
, value
):
762 Formats a number into a string with the requisite number of digits and
765 # Method moved to django.db.backends.util.
767 # It is preserved because it is used by the oracle backend
768 # (django.db.backends.oracle.query), and also for
769 # backwards-compatibility with any external code which may have used
771 from django
.db
.backends
import util
772 return util
.format_number(value
, self
.max_digits
, self
.decimal_places
)
774 def get_db_prep_save(self
, value
, connection
):
775 return connection
.ops
.value_to_db_decimal(self
.to_python(value
),
776 self
.max_digits
, self
.decimal_places
)
778 def get_prep_value(self
, value
):
779 return self
.to_python(value
)
781 def formfield(self
, **kwargs
):
783 'max_digits': self
.max_digits
,
784 'decimal_places': self
.decimal_places
,
785 'form_class': forms
.DecimalField
,
787 defaults
.update(kwargs
)
788 return super(DecimalField
, self
).formfield(**defaults
)
790 class EmailField(CharField
):
791 default_validators
= [validators
.validate_email
]
792 description
= _("E-mail address")
794 def __init__(self
, *args
, **kwargs
):
795 kwargs
['max_length'] = kwargs
.get('max_length', 75)
796 CharField
.__init
__(self
, *args
, **kwargs
)
798 class FilePathField(Field
):
799 description
= _("File path")
801 def __init__(self
, verbose_name
=None, name
=None, path
='', match
=None, recursive
=False, **kwargs
):
802 self
.path
, self
.match
, self
.recursive
= path
, match
, recursive
803 kwargs
['max_length'] = kwargs
.get('max_length', 100)
804 Field
.__init
__(self
, verbose_name
, name
, **kwargs
)
806 def formfield(self
, **kwargs
):
810 'recursive': self
.recursive
,
811 'form_class': forms
.FilePathField
,
813 defaults
.update(kwargs
)
814 return super(FilePathField
, self
).formfield(**defaults
)
816 def get_internal_type(self
):
817 return "FilePathField"
819 class FloatField(Field
):
820 empty_strings_allowed
= False
821 default_error_messages
= {
822 'invalid': _("This value must be a float."),
824 description
= _("Floating point number")
826 def get_prep_value(self
, value
):
831 def get_internal_type(self
):
834 def to_python(self
, value
):
839 except (TypeError, ValueError):
840 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
842 def formfield(self
, **kwargs
):
843 defaults
= {'form_class': forms
.FloatField
}
844 defaults
.update(kwargs
)
845 return super(FloatField
, self
).formfield(**defaults
)
847 class IntegerField(Field
):
848 empty_strings_allowed
= False
849 default_error_messages
= {
850 'invalid': _("This value must be an integer."),
852 description
= _("Integer")
854 def get_prep_value(self
, value
):
859 def get_prep_lookup(self
, lookup_type
, value
):
860 if (lookup_type
== 'gte' or lookup_type
== 'lt') \
861 and isinstance(value
, float):
862 value
= math
.ceil(value
)
863 return super(IntegerField
, self
).get_prep_lookup(lookup_type
, value
)
865 def get_internal_type(self
):
866 return "IntegerField"
868 def to_python(self
, value
):
873 except (TypeError, ValueError):
874 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
876 def formfield(self
, **kwargs
):
877 defaults
= {'form_class': forms
.IntegerField
}
878 defaults
.update(kwargs
)
879 return super(IntegerField
, self
).formfield(**defaults
)
881 class BigIntegerField(IntegerField
):
882 empty_strings_allowed
= False
883 description
= _("Big (8 byte) integer")
884 MAX_BIGINT
= 9223372036854775807
885 def get_internal_type(self
):
886 return "BigIntegerField"
888 def formfield(self
, **kwargs
):
889 defaults
= {'min_value': -BigIntegerField
.MAX_BIGINT
- 1,
890 'max_value': BigIntegerField
.MAX_BIGINT
}
891 defaults
.update(kwargs
)
892 return super(BigIntegerField
, self
).formfield(**defaults
)
894 class IPAddressField(Field
):
895 empty_strings_allowed
= False
896 description
= _("IP address")
897 def __init__(self
, *args
, **kwargs
):
898 kwargs
['max_length'] = 15
899 Field
.__init
__(self
, *args
, **kwargs
)
901 def get_internal_type(self
):
902 return "IPAddressField"
904 def formfield(self
, **kwargs
):
905 defaults
= {'form_class': forms
.IPAddressField
}
906 defaults
.update(kwargs
)
907 return super(IPAddressField
, self
).formfield(**defaults
)
909 class NullBooleanField(Field
):
910 empty_strings_allowed
= False
911 default_error_messages
= {
912 'invalid': _("This value must be either None, True or False."),
914 description
= _("Boolean (Either True, False or None)")
916 def __init__(self
, *args
, **kwargs
):
917 kwargs
['null'] = True
918 kwargs
['blank'] = True
919 Field
.__init
__(self
, *args
, **kwargs
)
921 def get_internal_type(self
):
922 return "NullBooleanField"
924 def to_python(self
, value
):
927 if value
in (True, False):
929 if value
in ('None',):
931 if value
in ('t', 'True', '1'):
933 if value
in ('f', 'False', '0'):
935 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
937 def get_prep_lookup(self
, lookup_type
, value
):
938 # Special-case handling for filters coming from a web request (e.g. the
939 # admin interface). Only works for scalar values (not lists). If you're
940 # passing in a list, you might as well make things the right type when
941 # constructing the list.
942 if value
in ('1', '0'):
943 value
= bool(int(value
))
944 return super(NullBooleanField
, self
).get_prep_lookup(lookup_type
, value
)
946 def get_prep_value(self
, value
):
951 def formfield(self
, **kwargs
):
953 'form_class': forms
.NullBooleanField
,
954 'required': not self
.blank
,
955 'label': capfirst(self
.verbose_name
),
956 'help_text': self
.help_text
}
957 defaults
.update(kwargs
)
958 return super(NullBooleanField
, self
).formfield(**defaults
)
960 class PositiveIntegerField(IntegerField
):
961 description
= _("Integer")
963 def get_internal_type(self
):
964 return "PositiveIntegerField"
966 def formfield(self
, **kwargs
):
967 defaults
= {'min_value': 0}
968 defaults
.update(kwargs
)
969 return super(PositiveIntegerField
, self
).formfield(**defaults
)
971 class PositiveSmallIntegerField(IntegerField
):
972 description
= _("Integer")
973 def get_internal_type(self
):
974 return "PositiveSmallIntegerField"
976 def formfield(self
, **kwargs
):
977 defaults
= {'min_value': 0}
978 defaults
.update(kwargs
)
979 return super(PositiveSmallIntegerField
, self
).formfield(**defaults
)
981 class SlugField(CharField
):
982 description
= _("String (up to %(max_length)s)")
983 def __init__(self
, *args
, **kwargs
):
984 kwargs
['max_length'] = kwargs
.get('max_length', 50)
985 # Set db_index=True unless it's been set manually.
986 if 'db_index' not in kwargs
:
987 kwargs
['db_index'] = True
988 super(SlugField
, self
).__init
__(*args
, **kwargs
)
990 def get_internal_type(self
):
993 def formfield(self
, **kwargs
):
994 defaults
= {'form_class': forms
.SlugField
}
995 defaults
.update(kwargs
)
996 return super(SlugField
, self
).formfield(**defaults
)
998 class SmallIntegerField(IntegerField
):
999 description
= _("Integer")
1001 def get_internal_type(self
):
1002 return "SmallIntegerField"
1004 class TextField(Field
):
1005 description
= _("Text")
1007 def get_internal_type(self
):
1010 def get_prep_value(self
, value
):
1011 if isinstance(value
, basestring
) or value
is None:
1013 return smart_unicode(value
)
1015 def formfield(self
, **kwargs
):
1016 defaults
= {'widget': forms
.Textarea
}
1017 defaults
.update(kwargs
)
1018 return super(TextField
, self
).formfield(**defaults
)
1020 class TimeField(Field
):
1021 description
= _("Time")
1023 empty_strings_allowed
= False
1024 default_error_messages
= {
1025 'invalid': _('Enter a valid time in HH:MM[:ss[.uuuuuu]] format.'),
1027 def __init__(self
, verbose_name
=None, name
=None, auto_now
=False, auto_now_add
=False, **kwargs
):
1028 self
.auto_now
, self
.auto_now_add
= auto_now
, auto_now_add
1029 if auto_now
or auto_now_add
:
1030 kwargs
['editable'] = False
1031 Field
.__init
__(self
, verbose_name
, name
, **kwargs
)
1033 def get_internal_type(self
):
1036 def to_python(self
, value
):
1039 if isinstance(value
, datetime
.time
):
1041 if isinstance(value
, datetime
.datetime
):
1042 # Not usually a good idea to pass in a datetime here (it loses
1043 # information), but this can be a side-effect of interacting with a
1044 # database backend (e.g. Oracle), so we'll be accommodating.
1047 # Attempt to parse a datetime:
1048 value
= smart_str(value
)
1049 # split usecs, because they are not recognized by strptime.
1052 value
, usecs
= value
.split('.')
1055 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
1058 kwargs
= {'microsecond': usecs
}
1060 try: # Seconds are optional, so try converting seconds first.
1061 return datetime
.time(*time
.strptime(value
, '%H:%M:%S')[3:6],
1064 try: # Try without seconds.
1065 return datetime
.time(*time
.strptime(value
, '%H:%M')[3:5],
1068 raise exceptions
.ValidationError(self
.error_messages
['invalid'])
1070 def pre_save(self
, model_instance
, add
):
1071 if self
.auto_now
or (self
.auto_now_add
and add
):
1072 value
= datetime
.datetime
.now().time()
1073 setattr(model_instance
, self
.attname
, value
)
1076 return super(TimeField
, self
).pre_save(model_instance
, add
)
1078 def get_prep_value(self
, value
):
1079 return self
.to_python(value
)
1081 def get_db_prep_value(self
, value
, connection
, prepared
=False):
1082 # Casts times into the format expected by the backend
1084 value
= self
.get_prep_value(value
)
1085 return connection
.ops
.value_to_db_time(value
)
1087 def value_to_string(self
, obj
):
1088 val
= self
._get
_val
_from
_obj
(obj
)
1092 data
= val
.strftime("%H:%M:%S")
1095 def formfield(self
, **kwargs
):
1096 defaults
= {'form_class': forms
.TimeField
}
1097 defaults
.update(kwargs
)
1098 return super(TimeField
, self
).formfield(**defaults
)
1100 class URLField(CharField
):
1101 description
= _("URL")
1103 def __init__(self
, verbose_name
=None, name
=None, verify_exists
=True, **kwargs
):
1104 kwargs
['max_length'] = kwargs
.get('max_length', 200)
1105 CharField
.__init
__(self
, verbose_name
, name
, **kwargs
)
1106 self
.validators
.append(validators
.URLValidator(verify_exists
=verify_exists
))
1108 class XMLField(TextField
):
1109 description
= _("XML text")
1111 def __init__(self
, verbose_name
=None, name
=None, schema_path
=None, **kwargs
):
1112 self
.schema_path
= schema_path
1113 Field
.__init
__(self
, verbose_name
, name
, **kwargs
)