Skip to content

Commit 92a0689

Browse files
author
nxexox
committed
add source attribute to fields
1 parent f96beb4 commit 92a0689

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

docs/api-guid/fields.md

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ A list of validator functions which should be applied to the incoming field inpu
4343

4444
A dictionary of error codes to error messages.
4545

46+
###`source`
47+
48+
The name of the field object with which the serializer field is associated. By default, the field name of the serializer = the name of the field of the object being serialized. This field changes this behavior.
49+
50+
**Example:**
51+
```python
52+
from rest_framework import serializers
53+
54+
class Model(object):
55+
field = 10
56+
57+
class StandardSerializer(serializers.Serializer):
58+
field = serializers.IntegerField()
59+
60+
class SourceFieldSerializer(serializers.Serializer):
61+
new_field = serializers.IntegerField(source='field')
62+
63+
print(StandardSerializer(instance=Model()).data)
64+
# {'field': 10}
65+
print(SourceFieldSerializer(instance=Model()).data)
66+
# {'new_field': 10}
67+
```
68+
4669
---
4770

4871
# Fields

docs/release-notes.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ Medium version numbers (0.x.0) may include API changes, in line with the [deprec
88

99
Major version numbers (x.0.0) are reserved for substantial project milestones.
1010

11+
### 0.3.4
12+
13+
**Date:** [22th Jule 2019]
14+
15+
* Added [`source`][SourceFieldAttribute] attribute for base fields class.
16+
1117
### 0.3.3
1218

1319
**Date:** [22th Jule 2019]
1420

1521
* Finish fix setup.py config. Change travis config, codecov config.
1622

17-
1823
### 0.3.2
1924

2025
**Date:** [22th Jule 2019]
@@ -118,6 +123,7 @@ Major version numbers (x.0.0) are reserved for substantial project milestones.
118123
* Push to Open Source community.
119124

120125

126+
[SourceFieldAttribute]: api-guid/fields#source
121127
[BaseViews]: api-guid/views/views
122128
[ViewsMixins]: api-guid/views/mixins
123129
[ViewsPaginations]: api-guid/views/paginations

rest_framework/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__/ |
99
|___/
1010
"""
11-
VERSION = (0, 3, 3)
11+
VERSION = (0, 3, 4)
1212

1313
__title__ = 'Python-Rest-Framework'
1414
__author__ = 'Deys Timofey'

rest_framework/serializers/fields.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class Field(object):
7171
}
7272
default_validators = [] # Default validators for field.
7373

74-
def __init__(self, required=True, default=None, label=None, validators=None, error_messages=None):
74+
def __init__(self, required=True, default=None, label=None, validators=None,
75+
error_messages=None, source=None):
7576
"""
7677
Base field.
7778
@@ -80,10 +81,12 @@ def __init__(self, required=True, default=None, label=None, validators=None, err
8081
:param str label: Field name.
8182
:param list validators: Validators for field.
8283
:param dict error_messages: Dictionary with custom error description.
84+
:param str source: Source field_name, if field_name object other in serializer.
8385
8486
"""
8587
self.label = label
8688
self.default = default
89+
self.source = source
8790
self.required = bool(required) if self.default is None else False
8891
# Added validator for check on required field.
8992
self._src_validators = validators
@@ -220,7 +223,7 @@ def _get_field_name(self):
220223
:rtype: str
221224
222225
"""
223-
return self.field_name
226+
return self.source or self.field_name
224227

225228
def _get_attribute(self, instance):
226229
"""

0 commit comments

Comments
 (0)