diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 439633c..877f841 100644
a
|
b
|
import copy
|
2 | 2 | import datetime |
3 | 3 | import os |
4 | 4 | import time |
| 5 | import warnings |
5 | 6 | try: |
6 | 7 | import decimal |
7 | 8 | except ImportError: |
… |
… |
def manipulator_validator_unique(f, opts, self, field_data, all_data):
|
46 | 47 | return |
47 | 48 | raise validators.ValidationError, _("%(optname)s with this %(fieldname)s already exists.") % {'optname': capfirst(opts.verbose_name), 'fieldname': f.verbose_name} |
48 | 49 | |
| 50 | def create_auto_field(base, value_func, always): |
| 51 | class Klass(base): |
| 52 | def __init__(self, *args, **kwargs): |
| 53 | kwargs['editable'] = False |
| 54 | kwargs['blank'] = True |
| 55 | super(base, self).__init__(self, *args, **kwargs) |
| 56 | |
| 57 | def pre_save(self, model_instance, add): |
| 58 | if always or add: |
| 59 | setattr(model_instance, self.attname, value_func()) |
| 60 | |
| 61 | return getattr(model_instance, self.attname) |
| 62 | |
49 | 63 | # A guide to Field parameters: |
50 | 64 | # |
51 | 65 | # * name: The name of the field specifed in the model. |
… |
… |
class Field(object):
|
416 | 430 | flat.append((choice,value)) |
417 | 431 | return flat |
418 | 432 | flatchoices = property(_get_flatchoices) |
419 | | |
| 433 | |
420 | 434 | def save_form_data(self, instance, data): |
421 | 435 | setattr(instance, self.name, data) |
422 | 436 | |
… |
… |
class DateField(Field):
|
531 | 545 | self.auto_now, self.auto_now_add = auto_now, auto_now_add |
532 | 546 | #HACKs : auto_now_add/auto_now should be done as a default or a pre_save. |
533 | 547 | if auto_now or auto_now_add: |
| 548 | self._issue_warning() |
534 | 549 | kwargs['editable'] = False |
535 | 550 | kwargs['blank'] = True |
536 | 551 | Field.__init__(self, verbose_name, name, **kwargs) |
… |
… |
class DateField(Field):
|
610 | 625 | defaults.update(kwargs) |
611 | 626 | return super(DateField, self).formfield(**defaults) |
612 | 627 | |
| 628 | def _issue_warning(self): |
| 629 | internal_type = self.get_internal_type() |
| 630 | warnings.warn( |
| 631 | message = "auto_now_add and auto_now are deprecated; use Created%s or Modified%s instead" % (internal_type, internal_type), |
| 632 | category = DeprecationWarning, |
| 633 | stacklevel = 4 |
| 634 | ) |
| 635 | |
| 636 | AutoAddDateField = create_auto_field( |
| 637 | base=DateField, |
| 638 | value_func=datetime.date.today, |
| 639 | always=False) |
| 640 | |
| 641 | AutoDateField = create_auto_field( |
| 642 | base=DateField, |
| 643 | value_func=datetime.date.today, |
| 644 | always=True) |
| 645 | |
613 | 646 | class DateTimeField(DateField): |
614 | 647 | def get_internal_type(self): |
615 | 648 | return "DateTimeField" |
… |
… |
class DateTimeField(DateField):
|
683 | 716 | defaults.update(kwargs) |
684 | 717 | return super(DateTimeField, self).formfield(**defaults) |
685 | 718 | |
| 719 | def _issue_warning(self): |
| 720 | internal_type = self.get_internal_type() |
| 721 | warnings.warn( |
| 722 | message = "auto_now_add and auto_now are deprecated; use Created%s and Modified%s" % (internal_type, internal_type), |
| 723 | category = DeprecationWarning, |
| 724 | stacklevel = 5 |
| 725 | ) |
| 726 | |
| 727 | AutoAddDateTimeField = create_auto_field( |
| 728 | base=DateTimeField, |
| 729 | value_func=datetime.datetime.now, |
| 730 | always=False) |
| 731 | |
| 732 | AutoDateTimeField = create_auto_field( |
| 733 | base=DateTimeField, |
| 734 | value_func=datetime.datetime.now, |
| 735 | always=True) |
| 736 | |
686 | 737 | class DecimalField(Field): |
687 | 738 | empty_strings_allowed = False |
688 | 739 | def __init__(self, verbose_name=None, name=None, max_digits=None, decimal_places=None, **kwargs): |
… |
… |
class TimeField(Field):
|
1152 | 1203 | defaults.update(kwargs) |
1153 | 1204 | return super(TimeField, self).formfield(**defaults) |
1154 | 1205 | |
| 1206 | AutoAddTimeField = create_auto_field( |
| 1207 | base=TimeField, |
| 1208 | value_func=lambda: datetime.datetime.now().time(), |
| 1209 | always=False) |
| 1210 | |
| 1211 | AutoTimeField = create_auto_field( |
| 1212 | base=TimeField, |
| 1213 | value_func=lambda: datetime.datetime.now().time(), |
| 1214 | always=True) |
| 1215 | |
1155 | 1216 | class URLField(CharField): |
1156 | 1217 | def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): |
1157 | 1218 | kwargs['max_length'] = kwargs.get('max_length', 200) |
diff --git a/docs/model-api.txt b/docs/model-api.txt
index 9a353c0..7c9d344 100644
a
|
b
|
class. Django uses the field class types to determine a few things:
|
116 | 116 | |
117 | 117 | Here are all available field types: |
118 | 118 | |
| 119 | ``AutoAddDateField`` |
| 120 | ~~~~~~~~~~~~~~~~~~~~ |
| 121 | |
| 122 | See `DateField`_ below. |
| 123 | |
| 124 | ``AutoAddDateTimeField`` |
| 125 | ~~~~~~~~~~~~~~~~~~~~ |
| 126 | |
| 127 | See `DateTimeField`_ below. |
| 128 | |
| 129 | ``AutoAddTimeField`` |
| 130 | ~~~~~~~~~~~~~~~~~~~~ |
| 131 | |
| 132 | See `TimeField`_ below. |
| 133 | |
| 134 | ``AutoDateField`` |
| 135 | ~~~~~~~~~~~~~~~~~~~~ |
| 136 | |
| 137 | See `DateField`_ below. |
| 138 | |
| 139 | ``AutoDateTimeField`` |
| 140 | ~~~~~~~~~~~~~~~~~~~~ |
| 141 | |
| 142 | See `DateTimeField`_ below. |
| 143 | |
119 | 144 | ``AutoField`` |
120 | 145 | ~~~~~~~~~~~~~ |
121 | 146 | |
… |
… |
You usually won't need to use this directly; a primary key field will
|
124 | 149 | automatically be added to your model if you don't specify otherwise. See |
125 | 150 | `Automatic primary key fields`_. |
126 | 151 | |
| 152 | ``AutoTimeField`` |
| 153 | ~~~~~~~~~~~~~~~~~~~~ |
| 154 | |
| 155 | See `TimeField`_ below. |
| 156 | |
127 | 157 | ``BooleanField`` |
128 | 158 | ~~~~~~~~~~~~~~~~ |
129 | 159 | |
… |
… |
A date field. Has a few extra optional arguments:
|
175 | 205 | override. |
176 | 206 | ====================== =================================================== |
177 | 207 | |
| 208 | In the Django development version, ``auto_now`` and ``auto_now_add`` are |
| 209 | deprecated in favor of ``AutoDateField`` and ``AutoAddDateField``. The former |
| 210 | replaces ``auto_now``, the latter ``auto_now_add`` while providing the same |
| 211 | functionality. |
| 212 | |
178 | 213 | The admin represents this as an ``<input type="text">`` with a JavaScript |
179 | 214 | calendar, and a shortcut for "Today." The JavaScript calendar will always start |
180 | 215 | the week on a Sunday. |
… |
… |
the week on a Sunday.
|
184 | 219 | |
185 | 220 | A date and time field. Takes the same extra options as ``DateField``. |
186 | 221 | |
| 222 | In the Django development version, ``auto_now`` and ``auto_now_add`` are deprecated |
| 223 | in favor of ``AutoDateTimeField`` and ``AutoAddDateTimeField``. The former |
| 224 | replaces ``auto_now``, the latter ``auto_now_add`` while providing the same |
| 225 | functionality. |
| 226 | |
187 | 227 | The admin represents this as two ``<input type="text">`` fields, with |
188 | 228 | JavaScript shortcuts. |
189 | 229 | |
… |
… |
be used for organizational purposes::
|
571 | 611 | ('unknown', 'Unknown'), |
572 | 612 | ) |
573 | 613 | |
574 | | The first element in each tuple is the name to apply to the group. The |
| 614 | The first element in each tuple is the name to apply to the group. The |
575 | 615 | second element is an iterable of 2-tuples, with each 2-tuple containing |
576 | | a value and a human-readable name for an option. Grouped options may be |
577 | | combined with ungrouped options within a single list (such as the |
| 616 | a value and a human-readable name for an option. Grouped options may be |
| 617 | combined with ungrouped options within a single list (such as the |
578 | 618 | `unknown` option in this example). |
579 | 619 | |
580 | 620 | For each model field that has ``choices`` set, Django will add a method to |