From a9d36d4726fc8eea02184b089ee6ed1d02e4c75e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Gro=C3=9F?= Date: Fri, 1 Mar 2013 15:03:27 +0100 Subject: [PATCH] Add docs update - part 1 --- docs/api-guide/fields.md | 24 ++++++++++++++++++------ docs/api-guide/settings.md | 35 +++++++++++++++++++++++++++++++++++ docs/topics/release-notes.md | 4 +--- rest_framework/fields.py | 18 +++++++++--------- 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index abacc1b82..c1f3c051e 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -185,18 +185,20 @@ Corresponds to `django.forms.fields.RegexField` A date representation. -Uses `DATE_INPUT_FORMATS` to validate date. - Optionally takes `format` as parameter to replace the matching pattern. Corresponds to `django.db.models.fields.DateField` +**Signature:** `DateField(input_formats=None, output_format=False)` + + - `input_formats` designates which input formats are supported. This will override the `DATE_INPUT_FORMATS` + + - `output_format` designates which output format will be used. This will override the `DATE_OUTPUT_FORMAT` + ## DateTimeField A date and time representation. -Uses `DATETIME_INPUT_FORMATS` to validate date_time. - Optionally takes `format` as parameter to replace the matching pattern. Corresponds to `django.db.models.fields.DateTimeField` @@ -211,16 +213,26 @@ If you want to override this behavior, you'll need to declare the `DateTimeField class Meta: model = Comment +**Signature:** `DateTimeField(input_formats=None, output_format=False)` + + - `input_formats` designates which input formats are supported. This will override the `DATETIME_INPUT_FORMATS` + + - `output_format` designates which output format will be used. This will override the `DATETIME_OUTPUT_FORMAT` + ## TimeField A time representation. -Uses `TIME_INPUT_FORMATS` to validate time. - Optionally takes `format` as parameter to replace the matching pattern. Corresponds to `django.db.models.fields.TimeField` +**Signature:** `TimeField(input_formats=None, output_format=False)` + + - `input_formats` designates which input formats are supported. This will override the `TIME_INPUT_FORMATS` + + - `output_format` designates which output format will be used. This will override the `TIME_OUTPUT_FORMAT` + ## IntegerField An integer representation. diff --git a/docs/api-guide/settings.md b/docs/api-guide/settings.md index e103fbab4..9080cacb0 100644 --- a/docs/api-guide/settings.md +++ b/docs/api-guide/settings.md @@ -174,4 +174,39 @@ The name of a parameter in the URL conf that may be used to provide a format suf Default: `'format'` +## DATE_INPUT_FORMATS + +Default: + + ( + '%Y-%m-%d', # '1984-07-31' + ) + +## DATE_OUTPUT_FORMAT + +## DATETIME_INPUT_FORMATS + +Default: + + ( + '%Y-%m-%d', # '1984-07-31' + '%Y-%m-%d %H:%M', # '1984-07-31 04:31' + '%Y-%m-%d %H:%M:%S', # '1984-07-31 04:31:59' + '%Y-%m-%d %H:%M:%S.%f', # '1984-07-31 04:31:59.000200' + ) + +## DATETIME_OUTPUT_FORMAT + +## TIME_INPUT_FORMATS + +Default: + + ( + '%H:%M', # '04:31' + '%H:%M:%S', # '04:31:59' + '%H:%M:%S.%f', # '04:31:59.000200' + ) + +## TIME_OUTPUT_FORMAT + [cite]: http://www.python.org/dev/peps/pep-0020/ diff --git a/docs/topics/release-notes.md b/docs/topics/release-notes.md index f60382acf..6b9e4e219 100644 --- a/docs/topics/release-notes.md +++ b/docs/topics/release-notes.md @@ -45,9 +45,7 @@ You can determine your currently installed version using `pip freeze`: * Request authentication is no longer lazily evaluated, instead authentication is always run, which results in more consistent, obvious behavior. Eg. Supplying bad auth credentials will now always return an error response, even if no permissions are set on the view. * Bugfix for serializer data being uncacheable with pickle protocol 0. * Bugfixes for model field validation edge-cases. -* Support `DATE_INPUT_FORMATS` for `DateField` validation -* Support `DATETIME_INPUT_FORMATS` for `DateTimeField` validation -* Support `TIME_INPUT_FORMATS` for `TimeField` validation +* Support for custom input and output formats for `DateField`, `DateTimeField` and `TimeField` ### 2.2.1 diff --git a/rest_framework/fields.py b/rest_framework/fields.py index bb77164a7..3eaa532ac 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -452,9 +452,9 @@ class DateField(WritableField): } empty = None - def __init__(self, *args, **kwargs): - self.input_formats = kwargs.pop('input_formats', api_settings.DATE_INPUT_FORMATS) - self.output_format = kwargs.pop('output_format', api_settings.DATE_OUTPUT_FORMAT) + def __init__(self, input_formats=None, output_format=None, *args, **kwargs): + self.input_formats = input_formats or api_settings.DATE_INPUT_FORMATS + self.output_format = output_format or api_settings.DATE_OUTPUT_FORMAT super(DateField, self).__init__(*args, **kwargs) def from_native(self, value): @@ -499,9 +499,9 @@ class DateTimeField(WritableField): } empty = None - def __init__(self, *args, **kwargs): - self.input_formats = kwargs.pop('input_formats', api_settings.DATETIME_INPUT_FORMATS) - self.output_format = kwargs.pop('output_format', api_settings.DATETIME_OUTPUT_FORMAT) + def __init__(self, input_formats=None, output_format=None, *args, **kwargs): + self.input_formats = input_formats or api_settings.DATETIME_INPUT_FORMATS + self.output_format = output_format or api_settings.DATETIME_OUTPUT_FORMAT super(DateTimeField, self).__init__(*args, **kwargs) def from_native(self, value): @@ -552,9 +552,9 @@ class TimeField(WritableField): } empty = None - def __init__(self, *args, **kwargs): - self.input_formats = kwargs.pop('input_formats', api_settings.TIME_INPUT_FORMATS) - self.output_format = kwargs.pop('output_format', api_settings.TIME_OUTPUT_FORMAT) + def __init__(self, input_formats=None, output_format=None, *args, **kwargs): + self.input_formats = input_formats or api_settings.TIME_INPUT_FORMATS + self.output_format = output_format or api_settings.TIME_OUTPUT_FORMAT super(TimeField, self).__init__(*args, **kwargs) def from_native(self, value):