Skip to content

Commit d985c7c

Browse files
bluetechtomchristie
authored andcommitted
Remove a few no longer needed compat checks and references (#7092)
* serializers: removes no longer needed compat checks UUIDField and DurationField are both supported in all supported Django versions. IPAddressField was removed in Django 1.9, which is no longer supported. * serializers: move related code closer together This way it's easier to see all of the mappings in one place. * serializers,docs: remove some DRF 2.x references The last release of DRF 2.x was 5 years ago, it seems fine to remove these references now.
1 parent de497a9 commit d985c7c

File tree

3 files changed

+6
-34
lines changed

3 files changed

+6
-34
lines changed

docs/api-guide/fields.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,6 @@ The `.to_representation()` method is called to convert the initial datatype into
597597

598598
The `to_internal_value()` method is called to restore a primitive datatype into its internal python representation. This method should raise a `serializers.ValidationError` if the data is invalid.
599599

600-
Note that the `WritableField` class that was present in version 2.x no longer exists. You should subclass `Field` and override `to_internal_value()` if the field supports data input.
601-
602600
## Examples
603601

604602
### A Basic Custom Field

docs/api-guide/generic-views.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ You can also use these hooks to provide additional validation, by raising a `Val
175175
raise ValidationError('You have already signed up')
176176
serializer.save(user=self.request.user)
177177

178-
**Note**: These methods replace the old-style version 2.x `pre_save`, `post_save`, `pre_delete` and `post_delete` methods, which are no longer available.
179-
180178
**Other methods**:
181179

182180
You won't typically need to override the following methods, although you might need to call into them if you're writing custom views using `GenericAPIView`.

rest_framework/serializers.py

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
2020
from django.core.exceptions import ValidationError as DjangoValidationError
2121
from django.db import models
22-
from django.db.models import DurationField as ModelDurationField
2322
from django.db.models.fields import Field as DjangoModelField
2423
from django.utils import timezone
2524
from django.utils.functional import cached_property
@@ -167,13 +166,6 @@ def create(self, validated_data):
167166
raise NotImplementedError('`create()` must be implemented.')
168167

169168
def save(self, **kwargs):
170-
assert not hasattr(self, 'save_object'), (
171-
'Serializer `%s.%s` has old-style version 2 `.save_object()` '
172-
'that is no longer compatible with REST framework 3. '
173-
'Use the new-style `.create()` and `.update()` methods instead.' %
174-
(self.__class__.__module__, self.__class__.__name__)
175-
)
176-
177169
assert hasattr(self, '_errors'), (
178170
'You must call `.is_valid()` before calling `.save()`.'
179171
)
@@ -217,13 +209,6 @@ def save(self, **kwargs):
217209
return self.instance
218210

219211
def is_valid(self, raise_exception=False):
220-
assert not hasattr(self, 'restore_object'), (
221-
'Serializer `%s.%s` has old-style version 2 `.restore_object()` '
222-
'that is no longer compatible with REST framework 3. '
223-
'Use the new-style `.create()` and `.update()` methods instead.' %
224-
(self.__class__.__module__, self.__class__.__name__)
225-
)
226-
227212
assert hasattr(self, 'initial_data'), (
228213
'Cannot call `.is_valid()` as no `data=` keyword argument was '
229214
'passed when instantiating the serializer instance.'
@@ -876,6 +861,7 @@ class ModelSerializer(Serializer):
876861
models.DateField: DateField,
877862
models.DateTimeField: DateTimeField,
878863
models.DecimalField: DecimalField,
864+
models.DurationField: DurationField,
879865
models.EmailField: EmailField,
880866
models.Field: ModelField,
881867
models.FileField: FileField,
@@ -890,11 +876,14 @@ class ModelSerializer(Serializer):
890876
models.TextField: CharField,
891877
models.TimeField: TimeField,
892878
models.URLField: URLField,
879+
models.UUIDField: UUIDField,
893880
models.GenericIPAddressField: IPAddressField,
894881
models.FilePathField: FilePathField,
895882
}
896-
if ModelDurationField is not None:
897-
serializer_field_mapping[ModelDurationField] = DurationField
883+
if postgres_fields:
884+
serializer_field_mapping[postgres_fields.HStoreField] = HStoreField
885+
serializer_field_mapping[postgres_fields.ArrayField] = ListField
886+
serializer_field_mapping[postgres_fields.JSONField] = JSONField
898887
serializer_related_field = PrimaryKeyRelatedField
899888
serializer_related_to_field = SlugRelatedField
900889
serializer_url_field = HyperlinkedIdentityField
@@ -1585,19 +1574,6 @@ def get_unique_for_date_validators(self):
15851574
return validators
15861575

15871576

1588-
if hasattr(models, 'UUIDField'):
1589-
ModelSerializer.serializer_field_mapping[models.UUIDField] = UUIDField
1590-
1591-
# IPAddressField is deprecated in Django
1592-
if hasattr(models, 'IPAddressField'):
1593-
ModelSerializer.serializer_field_mapping[models.IPAddressField] = IPAddressField
1594-
1595-
if postgres_fields:
1596-
ModelSerializer.serializer_field_mapping[postgres_fields.HStoreField] = HStoreField
1597-
ModelSerializer.serializer_field_mapping[postgres_fields.ArrayField] = ListField
1598-
ModelSerializer.serializer_field_mapping[postgres_fields.JSONField] = JSONField
1599-
1600-
16011577
class HyperlinkedModelSerializer(ModelSerializer):
16021578
"""
16031579
A type of `ModelSerializer` that uses hyperlinked relationships instead

0 commit comments

Comments
 (0)