mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-16 19:41:06 +03:00
Merge branch 'master' of https://github.com/tomchristie/django-rest-framework
This commit is contained in:
commit
702ec128ba
|
@ -214,10 +214,10 @@ In the case of JSON this means the default datetime representation uses the [ECM
|
|||
|
||||
**Signature:** `DateTimeField(format=None, input_formats=None)`
|
||||
|
||||
* `format` - A string representing the output format. If not specified, this defaults to `None`, which indicates that python `datetime` objects should be returned by `to_native`. In this case the datetime encoding will be determined by the renderer.
|
||||
* `format` - A string representing the output format. If not specified, this defaults to `None`, which indicates that Python `datetime` objects should be returned by `to_native`. In this case the datetime encoding will be determined by the renderer.
|
||||
* `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `DATETIME_INPUT_FORMATS` setting will be used, which defaults to `['iso-8601']`.
|
||||
|
||||
DateTime format strings may either be [python strftime formats][strftime] which explicitly specify the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style datetimes should be used. (eg `'2013-01-29T12:34:56.000000Z'`)
|
||||
DateTime format strings may either be [Python strftime formats][strftime] which explicitly specify the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style datetimes should be used. (eg `'2013-01-29T12:34:56.000000Z'`)
|
||||
|
||||
## DateField
|
||||
|
||||
|
@ -227,10 +227,10 @@ Corresponds to `django.db.models.fields.DateField`
|
|||
|
||||
**Signature:** `DateField(format=None, input_formats=None)`
|
||||
|
||||
* `format` - A string representing the output format. If not specified, this defaults to `None`, which indicates that python `date` objects should be returned by `to_native`. In this case the date encoding will be determined by the renderer.
|
||||
* `format` - A string representing the output format. If not specified, this defaults to `None`, which indicates that Python `date` objects should be returned by `to_native`. In this case the date encoding will be determined by the renderer.
|
||||
* `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `DATE_INPUT_FORMATS` setting will be used, which defaults to `['iso-8601']`.
|
||||
|
||||
Date format strings may either be [python strftime formats][strftime] which explicitly specify the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style dates should be used. (eg `'2013-01-29'`)
|
||||
Date format strings may either be [Python strftime formats][strftime] which explicitly specify the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style dates should be used. (eg `'2013-01-29'`)
|
||||
|
||||
## TimeField
|
||||
|
||||
|
@ -242,10 +242,10 @@ Corresponds to `django.db.models.fields.TimeField`
|
|||
|
||||
**Signature:** `TimeField(format=None, input_formats=None)`
|
||||
|
||||
* `format` - A string representing the output format. If not specified, this defaults to `None`, which indicates that python `time` objects should be returned by `to_native`. In this case the time encoding will be determined by the renderer.
|
||||
* `format` - A string representing the output format. If not specified, this defaults to `None`, which indicates that Python `time` objects should be returned by `to_native`. In this case the time encoding will be determined by the renderer.
|
||||
* `input_formats` - A list of strings representing the input formats which may be used to parse the date. If not specified, the `TIME_INPUT_FORMATS` setting will be used, which defaults to `['iso-8601']`.
|
||||
|
||||
Time format strings may either be [python strftime formats][strftime] which explicitly specify the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style times should be used. (eg `'12:34:56.000000'`)
|
||||
Time format strings may either be [Python strftime formats][strftime] which explicitly specify the format, or the special string `'iso-8601'`, which indicates that [ISO 8601][iso8601] style times should be used. (eg `'12:34:56.000000'`)
|
||||
|
||||
## IntegerField
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
REST framework supports HTTP content negotiation by providing a `Response` class which allows you to return content that can be rendered into multiple content types, depending on the client request.
|
||||
|
||||
The `Response` class subclasses Django's `SimpleTemplateResponse`. `Response` objects are initialised with data, which should consist of native python primatives. REST framework then uses standard HTTP content negotiation to determine how it should render the final response content.
|
||||
The `Response` class subclasses Django's `SimpleTemplateResponse`. `Response` objects are initialised with data, which should consist of native Python primitives. REST framework then uses standard HTTP content negotiation to determine how it should render the final response content.
|
||||
|
||||
There's no requirement for you to use the `Response` class, you can also return regular `HttpResponse` objects from your views if you want, but it provides a nicer interface for returning Web API responses.
|
||||
|
||||
|
@ -22,7 +22,7 @@ Unless you want to heavily customize REST framework for some reason, you should
|
|||
|
||||
**Signature:** `Response(data, status=None, template_name=None, headers=None, content_type=None)`
|
||||
|
||||
Unlike regular `HttpResponse` objects, you do not instantiate `Response` objects with rendered content. Instead you pass in unrendered data, which may consist of any python primatives.
|
||||
Unlike regular `HttpResponse` objects, you do not instantiate `Response` objects with rendered content. Instead you pass in unrendered data, which may consist of any Python primitives.
|
||||
|
||||
The renderers used by the `Response` class cannot natively handle complex datatypes such as Django model instances, so you need to serialize the data into primative datatypes before creating the `Response` object.
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ will take some serious design work.
|
|||
>
|
||||
> — Russell Keith-Magee, [Django users group][cite]
|
||||
|
||||
Serializers allow complex data such as querysets and model instances to be converted to native python datatypes that can then be easily rendered into `JSON`, `XML` or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
|
||||
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into `JSON`, `XML` or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
|
||||
|
||||
REST framework's serializers work very similarly to Django's `Form` and `ModelForm` classes. It provides a `Serializer` class which gives you a powerful, generic way to control the output of your responses, as well as a `ModelSerializer` class which provides a useful shortcut for creating serializers that deal with model instances and querysets.
|
||||
|
||||
|
@ -57,7 +57,7 @@ We can now use `CommentSerializer` to serialize a comment, or list of comments.
|
|||
serializer.data
|
||||
# {'email': u'leila@example.com', 'content': u'foo bar', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774)}
|
||||
|
||||
At this point we've translated the model instance into python native datatypes. To finalise the serialization process we render the data into `json`.
|
||||
At this point we've translated the model instance into Python native datatypes. To finalise the serialization process we render the data into `json`.
|
||||
|
||||
json = JSONRenderer().render(serializer.data)
|
||||
json
|
||||
|
@ -65,7 +65,7 @@ At this point we've translated the model instance into python native datatypes.
|
|||
|
||||
## Deserializing objects
|
||||
|
||||
Deserialization is similar. First we parse a stream into python native datatypes...
|
||||
Deserialization is similar. First we parse a stream into Python native datatypes...
|
||||
|
||||
stream = StringIO(json)
|
||||
data = JSONParser().parse(stream)
|
||||
|
|
|
@ -199,9 +199,9 @@ Default: `'format'`
|
|||
|
||||
#### DATETIME_FORMAT
|
||||
|
||||
A format string that should be used by default for rendering the output of `DateTimeField` serializer fields. If `None`, then `DateTimeField` serializer fields will return python `datetime` objects, and the datetime encoding will be determined by the renderer.
|
||||
A format string that should be used by default for rendering the output of `DateTimeField` serializer fields. If `None`, then `DateTimeField` serializer fields will return Python `datetime` objects, and the datetime encoding will be determined by the renderer.
|
||||
|
||||
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
|
||||
May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string.
|
||||
|
||||
Default: `None`
|
||||
|
||||
|
@ -209,15 +209,15 @@ Default: `None`
|
|||
|
||||
A list of format strings that should be used by default for parsing inputs to `DateTimeField` serializer fields.
|
||||
|
||||
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
|
||||
May be a list including the string `'iso-8601'` or Python [strftime format][strftime] strings.
|
||||
|
||||
Default: `['iso-8601']`
|
||||
|
||||
#### DATE_FORMAT
|
||||
|
||||
A format string that should be used by default for rendering the output of `DateField` serializer fields. If `None`, then `DateField` serializer fields will return python `date` objects, and the date encoding will be determined by the renderer.
|
||||
A format string that should be used by default for rendering the output of `DateField` serializer fields. If `None`, then `DateField` serializer fields will return Python `date` objects, and the date encoding will be determined by the renderer.
|
||||
|
||||
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
|
||||
May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string.
|
||||
|
||||
Default: `None`
|
||||
|
||||
|
@ -225,15 +225,15 @@ Default: `None`
|
|||
|
||||
A list of format strings that should be used by default for parsing inputs to `DateField` serializer fields.
|
||||
|
||||
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
|
||||
May be a list including the string `'iso-8601'` or Python [strftime format][strftime] strings.
|
||||
|
||||
Default: `['iso-8601']`
|
||||
|
||||
#### TIME_FORMAT
|
||||
|
||||
A format string that should be used by default for rendering the output of `TimeField` serializer fields. If `None`, then `TimeField` serializer fields will return python `time` objects, and the time encoding will be determined by the renderer.
|
||||
A format string that should be used by default for rendering the output of `TimeField` serializer fields. If `None`, then `TimeField` serializer fields will return Python `time` objects, and the time encoding will be determined by the renderer.
|
||||
|
||||
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
|
||||
May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string.
|
||||
|
||||
Default: `None`
|
||||
|
||||
|
@ -241,7 +241,7 @@ Default: `None`
|
|||
|
||||
A list of format strings that should be used by default for parsing inputs to `TimeField` serializer fields.
|
||||
|
||||
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
|
||||
May be a list including the string `'iso-8601'` or Python [strftime format][strftime] strings.
|
||||
|
||||
Default: `['iso-8601']`
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ The following packages are optional:
|
|||
* [django-oauth-plus][django-oauth-plus] (2.0+) and [oauth2][oauth2] (1.5.211+) - OAuth 1.0a support.
|
||||
* [django-oauth2-provider][django-oauth2-provider] (0.2.3+) - OAuth 2.0 support.
|
||||
|
||||
**Note**: The `oauth2` python package is badly misnamed, and actually provides OAuth 1.0a support. Also note that packages required for both OAuth 1.0a, and OAuth 2.0 are not yet Python 3 compatible.
|
||||
**Note**: The `oauth2` Python package is badly misnamed, and actually provides OAuth 1.0a support. Also note that packages required for both OAuth 1.0a, and OAuth 2.0 are not yet Python 3 compatible.
|
||||
|
||||
## Installation
|
||||
|
||||
|
|
|
@ -141,6 +141,7 @@ The following people have helped make REST framework great.
|
|||
* David Medina - [copitux]
|
||||
* Areski Belaid - [areski]
|
||||
* Ethan Freman - [mindlace]
|
||||
* David Sanders - [davesque]
|
||||
|
||||
Many thanks to everyone who's contributed to the project.
|
||||
|
||||
|
@ -318,3 +319,5 @@ You can also contact [@_tomchristie][twitter] directly on twitter.
|
|||
[copitux]: https://github.com/copitux
|
||||
[areski]: https://github.com/areski
|
||||
[mindlace]: https://github.com/mindlace
|
||||
[davesque]: https://github.com/davesque
|
||||
|
||||
|
|
|
@ -175,13 +175,13 @@ We've now got a few snippet instances to play with. Let's take a look at serial
|
|||
serializer.data
|
||||
# {'pk': 2, 'title': u'', 'code': u'print "hello, world"\n', 'linenos': False, 'language': u'python', 'style': u'friendly'}
|
||||
|
||||
At this point we've translated the model instance into python native datatypes. To finalize the serialization process we render the data into `json`.
|
||||
At this point we've translated the model instance into Python native datatypes. To finalize the serialization process we render the data into `json`.
|
||||
|
||||
content = JSONRenderer().render(serializer.data)
|
||||
content
|
||||
# '{"pk": 2, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'
|
||||
|
||||
Deserialization is similar. First we parse a stream into python native datatypes...
|
||||
Deserialization is similar. First we parse a stream into Python native datatypes...
|
||||
|
||||
import StringIO
|
||||
|
||||
|
|
|
@ -336,9 +336,13 @@ class ModelField(WritableField):
|
|||
raise ValueError("ModelField requires 'model_field' kwarg")
|
||||
|
||||
self.min_length = kwargs.pop('min_length',
|
||||
getattr(self.model_field, 'min_length', None))
|
||||
getattr(self.model_field, 'min_length', None))
|
||||
self.max_length = kwargs.pop('max_length',
|
||||
getattr(self.model_field, 'max_length', None))
|
||||
getattr(self.model_field, 'max_length', None))
|
||||
self.min_value = kwargs.pop('min_value',
|
||||
getattr(self.model_field, 'min_value', None))
|
||||
self.max_value = kwargs.pop('max_value',
|
||||
getattr(self.model_field, 'max_value', None))
|
||||
|
||||
super(ModelField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
@ -346,6 +350,10 @@ class ModelField(WritableField):
|
|||
self.validators.append(validators.MinLengthValidator(self.min_length))
|
||||
if self.max_length is not None:
|
||||
self.validators.append(validators.MaxLengthValidator(self.max_length))
|
||||
if self.min_value is not None:
|
||||
self.validators.append(validators.MinValueValidator(self.min_value))
|
||||
if self.max_value is not None:
|
||||
self.validators.append(validators.MaxValueValidator(self.max_value))
|
||||
|
||||
def from_native(self, value):
|
||||
rel = getattr(self.model_field, "rel", None)
|
||||
|
|
|
@ -212,7 +212,7 @@ class GenericAPIView(views.APIView):
|
|||
You may want to override this if you need to provide different
|
||||
serializations depending on the incoming request.
|
||||
|
||||
(Eg. admins get full serialization, others get basic serilization)
|
||||
(Eg. admins get full serialization, others get basic serialization)
|
||||
"""
|
||||
serializer_class = self.serializer_class
|
||||
if serializer_class is not None:
|
||||
|
|
|
@ -866,3 +866,33 @@ class FieldCallableDefault(TestCase):
|
|||
into = {}
|
||||
field.field_from_native({}, {}, 'field', into)
|
||||
self.assertEqual(into, {'field': 'foo bar'})
|
||||
|
||||
|
||||
class CustomIntegerField(TestCase):
|
||||
"""
|
||||
Test that custom fields apply min_value and max_value constraints
|
||||
"""
|
||||
def test_custom_fields_can_be_validated_for_value(self):
|
||||
|
||||
class MoneyField(models.PositiveIntegerField):
|
||||
pass
|
||||
|
||||
class EntryModel(models.Model):
|
||||
bank = MoneyField(validators=[validators.MaxValueValidator(100)])
|
||||
|
||||
class EntrySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = EntryModel
|
||||
|
||||
entry = EntryModel(bank=1)
|
||||
|
||||
serializer = EntrySerializer(entry, data={"bank": 11})
|
||||
self.assertTrue(serializer.is_valid())
|
||||
|
||||
serializer = EntrySerializer(entry, data={"bank": -1})
|
||||
self.assertFalse(serializer.is_valid())
|
||||
|
||||
serializer = EntrySerializer(entry, data={"bank": 101})
|
||||
self.assertFalse(serializer.is_valid())
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user