Ticket #7860: fix_supports_usecs.diff

File fix_supports_usecs.diff, 2.4 KB (added by wgj, 7 years ago)

fix supports usecs in DatetimeField, DateField, TimeField in get_db_prep_lookup

  • django/db/models/fields/__init__.py

     
    523523    def get_manipulator_field_objs(self):
    524524        return [oldforms.CommaSeparatedIntegerField]
    525525
     526def fix_supports_usecs(value):
     527    if value is not None:
     528        # SQLServer will throw a warning if microseconds are given, because it
     529        # doesn't support microseconds.
     530        if not connection.features.supports_usecs and hasattr(value, 'microsecond'):
     531            value = value.replace(microsecond=0)
     532    return value
     533
    526534class DateField(Field):
    527535    empty_strings_allowed = False
    528536    def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs):
     
    551559
    552560    def get_db_prep_lookup(self, lookup_type, value):
    553561        if lookup_type in ('range', 'in'):
    554             value = [smart_unicode(v) for v in value]
     562            value = [smart_unicode(fix_supports_usecs(v)) for v in value]
    555563        elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte') and hasattr(value, 'strftime'):
    556564            value = datetime_safe.new_date(value).strftime('%Y-%m-%d')
    557565        else:
    558             value = smart_unicode(value)
     566            value = smart_unicode(fix_supports_usecs(value))
    559567        return Field.get_db_prep_lookup(self, lookup_type, value)
    560568
    561569    def pre_save(self, model_instance, add):
     
    642650
    643651    def get_db_prep_lookup(self, lookup_type, value):
    644652        if lookup_type in ('range', 'in'):
    645             value = [smart_unicode(v) for v in value]
     653            value = [smart_unicode(fix_supports_usecs(v)) for v in value]
    646654        else:
    647             value = smart_unicode(value)
     655            value = smart_unicode(fix_supports_usecs(value))
    648656        return Field.get_db_prep_lookup(self, lookup_type, value)
    649657
    650658    def get_manipulator_field_objs(self):
     
    11071115        else:
    11081116            prep = smart_unicode
    11091117        if lookup_type in ('range', 'in'):
    1110             value = [prep(v) for v in value]
     1118            value = [prep(fix_supports_usecs(v)) for v in value]
    11111119        else:
    1112             value = prep(value)
     1120            value = prep(fix_supports_usecs(value))
    11131121        return Field.get_db_prep_lookup(self, lookup_type, value)
    11141122
    11151123    def pre_save(self, model_instance, add):
Back to Top