Skip to content

Commit 702ec12

Browse files
committed
2 parents 34f34f4 + 982411e commit 702ec12

File tree

10 files changed

+67
-26
lines changed

10 files changed

+67
-26
lines changed

docs/api-guide/fields.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ In the case of JSON this means the default datetime representation uses the [ECM
214214

215215
**Signature:** `DateTimeField(format=None, input_formats=None)`
216216

217-
* `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.
217+
* `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.
218218
* `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']`.
219219

220-
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'`)
220+
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'`)
221221

222222
## DateField
223223

@@ -227,10 +227,10 @@ Corresponds to `django.db.models.fields.DateField`
227227

228228
**Signature:** `DateField(format=None, input_formats=None)`
229229

230-
* `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.
230+
* `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.
231231
* `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']`.
232232

233-
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'`)
233+
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'`)
234234

235235
## TimeField
236236

@@ -242,10 +242,10 @@ Corresponds to `django.db.models.fields.TimeField`
242242

243243
**Signature:** `TimeField(format=None, input_formats=None)`
244244

245-
* `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.
245+
* `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.
246246
* `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']`.
247247

248-
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'`)
248+
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'`)
249249

250250
## IntegerField
251251

docs/api-guide/responses.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
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.
1010

11-
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.
11+
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.
1212

1313
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.
1414

@@ -22,7 +22,7 @@ Unless you want to heavily customize REST framework for some reason, you should
2222

2323
**Signature:** `Response(data, status=None, template_name=None, headers=None, content_type=None)`
2424

25-
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.
25+
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.
2626

2727
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.
2828

docs/api-guide/serializers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ will take some serious design work.
88
>
99
> — Russell Keith-Magee, [Django users group][cite]
1010
11-
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.
11+
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.
1212

1313
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.
1414

@@ -57,15 +57,15 @@ We can now use `CommentSerializer` to serialize a comment, or list of comments.
5757
serializer.data
5858
# {'email': u'[email protected]', 'content': u'foo bar', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774)}
5959

60-
At this point we've translated the model instance into python native datatypes. To finalise the serialization process we render the data into `json`.
60+
At this point we've translated the model instance into Python native datatypes. To finalise the serialization process we render the data into `json`.
6161

6262
json = JSONRenderer().render(serializer.data)
6363
json
6464
# '{"email": "[email protected]", "content": "foo bar", "created": "2012-08-22T16:20:09.822"}'
6565

6666
## Deserializing objects
6767
68-
Deserialization is similar. First we parse a stream into python native datatypes...
68+
Deserialization is similar. First we parse a stream into Python native datatypes...
6969

7070
stream = StringIO(json)
7171
data = JSONParser().parse(stream)

docs/api-guide/settings.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,49 +199,49 @@ Default: `'format'`
199199

200200
#### DATETIME_FORMAT
201201

202-
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.
202+
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.
203203

204-
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
204+
May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string.
205205

206206
Default: `None`
207207

208208
#### DATETIME_INPUT_FORMATS
209209

210210
A list of format strings that should be used by default for parsing inputs to `DateTimeField` serializer fields.
211211

212-
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
212+
May be a list including the string `'iso-8601'` or Python [strftime format][strftime] strings.
213213

214214
Default: `['iso-8601']`
215215

216216
#### DATE_FORMAT
217217

218-
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.
218+
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.
219219

220-
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
220+
May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string.
221221

222222
Default: `None`
223223

224224
#### DATE_INPUT_FORMATS
225225

226226
A list of format strings that should be used by default for parsing inputs to `DateField` serializer fields.
227227

228-
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
228+
May be a list including the string `'iso-8601'` or Python [strftime format][strftime] strings.
229229

230230
Default: `['iso-8601']`
231231

232232
#### TIME_FORMAT
233233

234-
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.
234+
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.
235235

236-
May be any of `None`, `'iso-8601'` or a python [strftime format][strftime] string.
236+
May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string.
237237

238238
Default: `None`
239239

240240
#### TIME_INPUT_FORMATS
241241

242242
A list of format strings that should be used by default for parsing inputs to `TimeField` serializer fields.
243243

244-
May be a list including the string `'iso-8601'` or python [strftime format][strftime] strings.
244+
May be a list including the string `'iso-8601'` or Python [strftime format][strftime] strings.
245245

246246
Default: `['iso-8601']`
247247

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The following packages are optional:
4343
* [django-oauth-plus][django-oauth-plus] (2.0+) and [oauth2][oauth2] (1.5.211+) - OAuth 1.0a support.
4444
* [django-oauth2-provider][django-oauth2-provider] (0.2.3+) - OAuth 2.0 support.
4545

46-
**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.
46+
**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.
4747

4848
## Installation
4949

docs/topics/credits.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ The following people have helped make REST framework great.
141141
* David Medina - [copitux]
142142
* Areski Belaid - [areski]
143143
* Ethan Freman - [mindlace]
144+
* David Sanders - [davesque]
144145

145146
Many thanks to everyone who's contributed to the project.
146147

@@ -318,3 +319,5 @@ You can also contact [@_tomchristie][twitter] directly on twitter.
318319
[copitux]: https://github.com/copitux
319320
[areski]: https://github.com/areski
320321
[mindlace]: https://github.com/mindlace
322+
[davesque]: https://github.com/davesque
323+

docs/tutorial/1-serialization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ We've now got a few snippet instances to play with. Let's take a look at serial
175175
serializer.data
176176
# {'pk': 2, 'title': u'', 'code': u'print "hello, world"\n', 'linenos': False, 'language': u'python', 'style': u'friendly'}
177177

178-
At this point we've translated the model instance into python native datatypes. To finalize the serialization process we render the data into `json`.
178+
At this point we've translated the model instance into Python native datatypes. To finalize the serialization process we render the data into `json`.
179179

180180
content = JSONRenderer().render(serializer.data)
181181
content
182182
# '{"pk": 2, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'
183183

184-
Deserialization is similar. First we parse a stream into python native datatypes...
184+
Deserialization is similar. First we parse a stream into Python native datatypes...
185185

186186
import StringIO
187187

rest_framework/fields.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,24 @@ def __init__(self, *args, **kwargs):
336336
raise ValueError("ModelField requires 'model_field' kwarg")
337337

338338
self.min_length = kwargs.pop('min_length',
339-
getattr(self.model_field, 'min_length', None))
339+
getattr(self.model_field, 'min_length', None))
340340
self.max_length = kwargs.pop('max_length',
341-
getattr(self.model_field, 'max_length', None))
341+
getattr(self.model_field, 'max_length', None))
342+
self.min_value = kwargs.pop('min_value',
343+
getattr(self.model_field, 'min_value', None))
344+
self.max_value = kwargs.pop('max_value',
345+
getattr(self.model_field, 'max_value', None))
342346

343347
super(ModelField, self).__init__(*args, **kwargs)
344348

345349
if self.min_length is not None:
346350
self.validators.append(validators.MinLengthValidator(self.min_length))
347351
if self.max_length is not None:
348352
self.validators.append(validators.MaxLengthValidator(self.max_length))
353+
if self.min_value is not None:
354+
self.validators.append(validators.MinValueValidator(self.min_value))
355+
if self.max_value is not None:
356+
self.validators.append(validators.MaxValueValidator(self.max_value))
349357

350358
def from_native(self, value):
351359
rel = getattr(self.model_field, "rel", None)

rest_framework/generics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def get_serializer_class(self):
212212
You may want to override this if you need to provide different
213213
serializations depending on the incoming request.
214214
215-
(Eg. admins get full serialization, others get basic serilization)
215+
(Eg. admins get full serialization, others get basic serialization)
216216
"""
217217
serializer_class = self.serializer_class
218218
if serializer_class is not None:

rest_framework/tests/test_fields.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,33 @@ def test_default_can_be_simple_callable(self):
866866
into = {}
867867
field.field_from_native({}, {}, 'field', into)
868868
self.assertEqual(into, {'field': 'foo bar'})
869+
870+
871+
class CustomIntegerField(TestCase):
872+
"""
873+
Test that custom fields apply min_value and max_value constraints
874+
"""
875+
def test_custom_fields_can_be_validated_for_value(self):
876+
877+
class MoneyField(models.PositiveIntegerField):
878+
pass
879+
880+
class EntryModel(models.Model):
881+
bank = MoneyField(validators=[validators.MaxValueValidator(100)])
882+
883+
class EntrySerializer(serializers.ModelSerializer):
884+
class Meta:
885+
model = EntryModel
886+
887+
entry = EntryModel(bank=1)
888+
889+
serializer = EntrySerializer(entry, data={"bank": 11})
890+
self.assertTrue(serializer.is_valid())
891+
892+
serializer = EntrySerializer(entry, data={"bank": -1})
893+
self.assertFalse(serializer.is_valid())
894+
895+
serializer = EntrySerializer(entry, data={"bank": 101})
896+
self.assertFalse(serializer.is_valid())
897+
898+

0 commit comments

Comments
 (0)