Django

Code

Ticket #6434: 6434-dyn.patch

File 6434-dyn.patch, 6.7 kB (added by mk, 6 months ago)
  • a/django/db/models/fields/__init__.py

    old new  
    22import datetime 
    33import os 
    44import time 
     5import warnings 
    56try: 
    67    import decimal 
    78except ImportError: 
     
    4647        return 
    4748    raise validators.ValidationError, _("%(optname)s with this %(fieldname)s already exists.") % {'optname': capfirst(opts.verbose_name), 'fieldname': f.verbose_name} 
    4849 
     50def 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 
    4963# A guide to Field parameters: 
    5064# 
    5165#   * name:      The name of the field specifed in the model. 
     
    416430                flat.append((choice,value)) 
    417431        return flat 
    418432    flatchoices = property(_get_flatchoices) 
    419      
     433 
    420434    def save_form_data(self, instance, data): 
    421435        setattr(instance, self.name, data) 
    422436 
     
    531545        self.auto_now, self.auto_now_add = auto_now, auto_now_add 
    532546        #HACKs : auto_now_add/auto_now should be done as a default or a pre_save. 
    533547        if auto_now or auto_now_add: 
     548            self._issue_warning() 
    534549            kwargs['editable'] = False 
    535550            kwargs['blank'] = True 
    536551        Field.__init__(self, verbose_name, name, **kwargs) 
     
    610625        defaults.update(kwargs) 
    611626        return super(DateField, self).formfield(**defaults) 
    612627 
     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 
     636AutoAddDateField = create_auto_field( 
     637    base=DateField, 
     638    value_func=datetime.date.today, 
     639    always=False) 
     640 
     641AutoDateField = create_auto_field( 
     642    base=DateField, 
     643    value_func=datetime.date.today, 
     644    always=True) 
     645 
    613646class DateTimeField(DateField): 
    614647    def get_internal_type(self): 
    615648        return "DateTimeField" 
     
    683716        defaults.update(kwargs) 
    684717        return super(DateTimeField, self).formfield(**defaults) 
    685718 
     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 
     727AutoAddDateTimeField = create_auto_field( 
     728    base=DateTimeField, 
     729    value_func=datetime.datetime.now, 
     730    always=False) 
     731 
     732AutoDateTimeField = create_auto_field( 
     733    base=DateTimeField, 
     734    value_func=datetime.datetime.now, 
     735    always=True) 
     736 
    686737class DecimalField(Field): 
    687738    empty_strings_allowed = False 
    688739    def __init__(self, verbose_name=None, name=None, max_digits=None, decimal_places=None, **kwargs): 
     
    11521203        defaults.update(kwargs) 
    11531204        return super(TimeField, self).formfield(**defaults) 
    11541205 
     1206AutoAddTimeField = create_auto_field( 
     1207    base=TimeField, 
     1208    value_func=lambda: datetime.datetime.now().time(), 
     1209    always=False) 
     1210 
     1211AutoTimeField = create_auto_field( 
     1212    base=TimeField, 
     1213    value_func=lambda: datetime.datetime.now().time(), 
     1214    always=True) 
     1215 
    11551216class URLField(CharField): 
    11561217    def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs): 
    11571218        kwargs['max_length'] = kwargs.get('max_length', 200) 
  • a/docs/model-api.txt

    old new  
    116116 
    117117Here are all available field types: 
    118118 
     119``AutoAddDateField`` 
     120~~~~~~~~~~~~~~~~~~~~ 
     121 
     122See `DateField`_ below. 
     123 
     124``AutoAddDateTimeField`` 
     125~~~~~~~~~~~~~~~~~~~~ 
     126 
     127See `DateTimeField`_ below. 
     128 
     129``AutoAddTimeField`` 
     130~~~~~~~~~~~~~~~~~~~~ 
     131 
     132See `TimeField`_ below. 
     133 
     134``AutoDateField`` 
     135~~~~~~~~~~~~~~~~~~~~ 
     136 
     137See `DateField`_ below. 
     138 
     139``AutoDateTimeField`` 
     140~~~~~~~~~~~~~~~~~~~~ 
     141 
     142See `DateTimeField`_ below. 
     143 
    119144``AutoField`` 
    120145~~~~~~~~~~~~~ 
    121146 
     
    124149automatically be added to your model if you don't specify otherwise. See 
    125150`Automatic primary key fields`_. 
    126151 
     152``AutoTimeField`` 
     153~~~~~~~~~~~~~~~~~~~~ 
     154 
     155See `TimeField`_ below. 
     156 
    127157``BooleanField`` 
    128158~~~~~~~~~~~~~~~~ 
    129159 
     
    175205                            override. 
    176206    ======================  =================================================== 
    177207 
     208In the Django development version, ``auto_now`` and ``auto_now_add`` are 
     209deprecated in favor of ``AutoDateField`` and ``AutoAddDateField``. The former 
     210replaces ``auto_now``, the latter ``auto_now_add`` while providing the same 
     211functionality. 
     212 
    178213The admin represents this as an ``<input type="text">`` with a JavaScript 
    179214calendar, and a shortcut for "Today."  The JavaScript calendar will always start 
    180215the week on a Sunday. 
     
    184219 
    185220A date and time field. Takes the same extra options as ``DateField``. 
    186221 
     222In the Django development version, ``auto_now`` and ``auto_now_add`` are deprecated 
     223in favor of ``AutoDateTimeField`` and ``AutoAddDateTimeField``. The former 
     224replaces ``auto_now``, the latter ``auto_now_add`` while providing the same 
     225functionality. 
     226 
    187227The admin represents this as two ``<input type="text">`` fields, with 
    188228JavaScript shortcuts. 
    189229 
     
    571611        ('unknown', 'Unknown'), 
    572612    ) 
    573613 
    574 The first element in each tuple is the name to apply to the group. The  
     614The first element in each tuple is the name to apply to the group. The 
    575615second 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  
     616a value and a human-readable name for an option. Grouped options may be 
     617combined with ungrouped options within a single list (such as the 
    578618`unknown` option in this example). 
    579619 
    580620For each model field that has ``choices`` set, Django will add a method to