mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Support format=None for date/time fields
This commit is contained in:
parent
4db23cae21
commit
5586b6581d
|
@ -548,8 +548,8 @@ class DateField(Field):
|
|||
format = api_settings.DATE_FORMAT
|
||||
input_formats = api_settings.DATE_INPUT_FORMATS
|
||||
|
||||
def __init__(self, format=None, input_formats=None, *args, **kwargs):
|
||||
self.format = format if format is not None else self.format
|
||||
def __init__(self, format=empty, input_formats=None, *args, **kwargs):
|
||||
self.format = format if format is not empty else self.format
|
||||
self.input_formats = input_formats if input_formats is not None else self.input_formats
|
||||
super(DateField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
@ -604,8 +604,8 @@ class DateTimeField(Field):
|
|||
input_formats = api_settings.DATETIME_INPUT_FORMATS
|
||||
default_timezone = timezone.get_default_timezone() if settings.USE_TZ else None
|
||||
|
||||
def __init__(self, format=None, input_formats=None, default_timezone=None, *args, **kwargs):
|
||||
self.format = format if format is not None else self.format
|
||||
def __init__(self, format=empty, input_formats=None, default_timezone=None, *args, **kwargs):
|
||||
self.format = format if format is not empty else self.format
|
||||
self.input_formats = input_formats if input_formats is not None else self.input_formats
|
||||
self.default_timezone = default_timezone if default_timezone is not None else self.default_timezone
|
||||
super(DateTimeField, self).__init__(*args, **kwargs)
|
||||
|
@ -670,8 +670,8 @@ class TimeField(Field):
|
|||
format = api_settings.TIME_FORMAT
|
||||
input_formats = api_settings.TIME_INPUT_FORMATS
|
||||
|
||||
def __init__(self, format=None, input_formats=None, *args, **kwargs):
|
||||
self.format = format if format is not None else self.format
|
||||
def __init__(self, format=empty, input_formats=None, *args, **kwargs):
|
||||
self.format = format if format is not empty else self.format
|
||||
self.input_formats = input_formats if input_formats is not None else self.input_formats
|
||||
super(TimeField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
|
|
@ -316,7 +316,9 @@ class TestDateField(FieldValues):
|
|||
'2001-99-99': ['Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]'],
|
||||
datetime.datetime(2001, 1, 1, 12, 00): ['Expected a date but got a datetime.'],
|
||||
}
|
||||
outputs = {}
|
||||
outputs = {
|
||||
datetime.date(2001, 1, 1): '2001-01-01',
|
||||
}
|
||||
field = fields.DateField()
|
||||
|
||||
|
||||
|
@ -334,6 +336,30 @@ class TestCustomInputFormatDateField(FieldValues):
|
|||
field = fields.DateField(input_formats=['%d %b %Y'])
|
||||
|
||||
|
||||
class TestCustomOutputFormatDateField(FieldValues):
|
||||
"""
|
||||
Values for `DateField` with a custom output format.
|
||||
"""
|
||||
valid_inputs = {}
|
||||
invalid_inputs = {}
|
||||
outputs = {
|
||||
datetime.date(2001, 1, 1): '01 Jan 2001'
|
||||
}
|
||||
field = fields.DateField(format='%d %b %Y')
|
||||
|
||||
|
||||
class TestNoOutputFormatDateField(FieldValues):
|
||||
"""
|
||||
Values for `DateField` with no output format.
|
||||
"""
|
||||
valid_inputs = {}
|
||||
invalid_inputs = {}
|
||||
outputs = {
|
||||
datetime.date(2001, 1, 1): datetime.date(2001, 1, 1)
|
||||
}
|
||||
field = fields.DateField(format=None)
|
||||
|
||||
|
||||
class TestDateTimeField(FieldValues):
|
||||
"""
|
||||
Valid and invalid values for `DateTimeField`.
|
||||
|
@ -351,7 +377,10 @@ class TestDateTimeField(FieldValues):
|
|||
'2001-99-99T99:00': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'],
|
||||
datetime.date(2001, 1, 1): ['Expected a datetime but got a date.'],
|
||||
}
|
||||
outputs = {}
|
||||
outputs = {
|
||||
datetime.datetime(2001, 1, 1, 13, 00): '2001-01-01T13:00:00',
|
||||
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): '2001-01-01T13:00:00Z',
|
||||
}
|
||||
field = fields.DateTimeField(default_timezone=timezone.UTC())
|
||||
|
||||
|
||||
|
@ -369,6 +398,30 @@ class TestCustomInputFormatDateTimeField(FieldValues):
|
|||
field = fields.DateTimeField(default_timezone=timezone.UTC(), input_formats=['%I:%M%p, %d %b %Y'])
|
||||
|
||||
|
||||
class TestCustomOutputFormatDateTimeField(FieldValues):
|
||||
"""
|
||||
Values for `DateTimeField` with a custom output format.
|
||||
"""
|
||||
valid_inputs = {}
|
||||
invalid_inputs = {}
|
||||
outputs = {
|
||||
datetime.datetime(2001, 1, 1, 13, 00): '01:00PM, 01 Jan 2001',
|
||||
}
|
||||
field = fields.DateTimeField(format='%I:%M%p, %d %b %Y')
|
||||
|
||||
|
||||
class TestNoOutputFormatDateTimeField(FieldValues):
|
||||
"""
|
||||
Values for `DateTimeField` with no output format.
|
||||
"""
|
||||
valid_inputs = {}
|
||||
invalid_inputs = {}
|
||||
outputs = {
|
||||
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00),
|
||||
}
|
||||
field = fields.DateTimeField(format=None)
|
||||
|
||||
|
||||
class TestNaiveDateTimeField(FieldValues):
|
||||
"""
|
||||
Valid and invalid values for `DateTimeField` with naive datetimes.
|
||||
|
@ -394,7 +447,9 @@ class TestTimeField(FieldValues):
|
|||
'abc': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'],
|
||||
'99:99': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]]'],
|
||||
}
|
||||
outputs = {}
|
||||
outputs = {
|
||||
datetime.time(13, 00): '13:00:00'
|
||||
}
|
||||
field = fields.TimeField()
|
||||
|
||||
|
||||
|
@ -412,6 +467,30 @@ class TestCustomInputFormatTimeField(FieldValues):
|
|||
field = fields.TimeField(input_formats=['%I:%M%p'])
|
||||
|
||||
|
||||
class TestCustomOutputFormatTimeField(FieldValues):
|
||||
"""
|
||||
Values for `TimeField` with a custom output format.
|
||||
"""
|
||||
valid_inputs = {}
|
||||
invalid_inputs = {}
|
||||
outputs = {
|
||||
datetime.time(13, 00): '01:00PM'
|
||||
}
|
||||
field = fields.TimeField(format='%I:%M%p')
|
||||
|
||||
|
||||
class TestNoOutputFormatTimeField(FieldValues):
|
||||
"""
|
||||
Values for `TimeField` with a no output format.
|
||||
"""
|
||||
valid_inputs = {}
|
||||
invalid_inputs = {}
|
||||
outputs = {
|
||||
datetime.time(13, 00): datetime.time(13, 00)
|
||||
}
|
||||
field = fields.TimeField(format=None)
|
||||
|
||||
|
||||
# Choice types...
|
||||
|
||||
class TestChoiceField(FieldValues):
|
||||
|
|
Loading…
Reference in New Issue
Block a user