Skip to content

Commit b5d753b

Browse files
authored
add __deepcopy__ on all serializers (#1)
1 parent 1af7fdb commit b5d753b

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

README.md

+29-22
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,51 @@ Full documentation for the project is available at [https://nxexox.github.io/pyt
1818

1919
Install using `pip`, including any optional packages you want...
2020

21-
pip install python-rest-framework
21+
```bash
22+
pip install python-rest-framework
23+
```
2224

2325
...or clone the project from github.
2426

25-
git clone [email protected]:nxexox/python-rest-framework.git
26-
27+
```bash
28+
git clone [email protected]:nxexox/python-rest-framework.git
29+
```
2730

2831
## Example
2932

3033
For example, we will serialize the data from the request object.
3134

3235
First we write the serializer
3336

34-
from rest_framework.serializers import (
35-
Serializer, CharField, IntegerField, ListField, FloatField
36-
)
37+
```python
38+
from rest_framework.serializers import (
39+
Serializer, CharField, IntegerField, ListField, FloatField
40+
)
3741

38-
# Example serializer for parsing body data from web request.
39-
class ExampleSerializer(Serializer):
40-
char_field = CharField(label='This char field', required=True)
41-
int_field = IntegerField(label='This int field', required=True)
42-
list_float_field = ListField(child=FloatField(), required=True, min_length=2)
42+
# Example serializer for parsing body data from web request.
43+
class ExampleSerializer(Serializer):
44+
char_field = CharField(label='This char field', required=True)
45+
int_field = IntegerField(label='This int field', required=True)
46+
list_float_field = ListField(child=FloatField(), required=True, min_length=2)
47+
```
4348

4449
---
4550

4651
Now we process the request body with a serializer
4752

48-
# web request data
49-
data = {
50-
'char_field': 'example', 'int_field': 1,
51-
'list_float_field': [1.0, 1.1, 1.2]
52-
}
53-
54-
ser = ExampleSerializer(data=data)
55-
if ser.is_valid():
56-
print(ser.validated_data)
57-
else:
58-
print(ser.errors)
53+
```python
54+
# web request data
55+
data = {
56+
'char_field': 'example', 'int_field': 1,
57+
'list_float_field': [1.0, 1.1, 1.2]
58+
}
59+
60+
ser = ExampleSerializer(data=data)
61+
if ser.is_valid():
62+
print(ser.validated_data)
63+
else:
64+
print(ser.errors)
65+
```
5966

6067
[docs]: https://nxexox.github.io/python-rest-framework/
6168
[pypi-version]: https://img.shields.io/pypi/v/python-rest-framework.svg

rest_framework/serializers/serializers.py

+9
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ def __new__(cls, *args, **kwargs):
125125
return cls.many_init(*args, **kwargs)
126126
return super(BaseSerializer, cls).__new__(cls)
127127

128+
def __deepcopy__(self, memo={}):
129+
return self.__class__(instance=self.instance, data=self.data)
130+
128131
@classmethod
129132
def many_init(cls, *args, **kwargs):
130133
"""
@@ -501,6 +504,12 @@ def __init__(self, child=None, allow_empty=None, *args, **kwargs):
501504
# Bind child serializer.
502505
self.child.bind(field_name='', parent=self)
503506

507+
def __deepcopy__(self, memo={}):
508+
return self.__class__(
509+
instance=self.instance, data=self.data,
510+
child=self.child, allow_empty=self.allow_empty
511+
)
512+
504513
def to_internal_value(self, data):
505514
"""
506515
Data transformation to python list object.

0 commit comments

Comments
 (0)