mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-26 16:30:41 +03:00
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.
This commit is contained in:
parent
de497a9bf1
commit
d985c7cbb9
|
@ -597,8 +597,6 @@ The `.to_representation()` method is called to convert the initial datatype into
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### A Basic Custom Field
|
### A Basic Custom Field
|
||||||
|
|
|
@ -175,8 +175,6 @@ You can also use these hooks to provide additional validation, by raising a `Val
|
||||||
raise ValidationError('You have already signed up')
|
raise ValidationError('You have already signed up')
|
||||||
serializer.save(user=self.request.user)
|
serializer.save(user=self.request.user)
|
||||||
|
|
||||||
**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.
|
|
||||||
|
|
||||||
**Other methods**:
|
**Other methods**:
|
||||||
|
|
||||||
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`.
|
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`.
|
||||||
|
|
|
@ -19,7 +19,6 @@ from collections.abc import Mapping
|
||||||
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
|
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
|
||||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import DurationField as ModelDurationField
|
|
||||||
from django.db.models.fields import Field as DjangoModelField
|
from django.db.models.fields import Field as DjangoModelField
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
|
@ -167,13 +166,6 @@ class BaseSerializer(Field):
|
||||||
raise NotImplementedError('`create()` must be implemented.')
|
raise NotImplementedError('`create()` must be implemented.')
|
||||||
|
|
||||||
def save(self, **kwargs):
|
def save(self, **kwargs):
|
||||||
assert not hasattr(self, 'save_object'), (
|
|
||||||
'Serializer `%s.%s` has old-style version 2 `.save_object()` '
|
|
||||||
'that is no longer compatible with REST framework 3. '
|
|
||||||
'Use the new-style `.create()` and `.update()` methods instead.' %
|
|
||||||
(self.__class__.__module__, self.__class__.__name__)
|
|
||||||
)
|
|
||||||
|
|
||||||
assert hasattr(self, '_errors'), (
|
assert hasattr(self, '_errors'), (
|
||||||
'You must call `.is_valid()` before calling `.save()`.'
|
'You must call `.is_valid()` before calling `.save()`.'
|
||||||
)
|
)
|
||||||
|
@ -217,13 +209,6 @@ class BaseSerializer(Field):
|
||||||
return self.instance
|
return self.instance
|
||||||
|
|
||||||
def is_valid(self, raise_exception=False):
|
def is_valid(self, raise_exception=False):
|
||||||
assert not hasattr(self, 'restore_object'), (
|
|
||||||
'Serializer `%s.%s` has old-style version 2 `.restore_object()` '
|
|
||||||
'that is no longer compatible with REST framework 3. '
|
|
||||||
'Use the new-style `.create()` and `.update()` methods instead.' %
|
|
||||||
(self.__class__.__module__, self.__class__.__name__)
|
|
||||||
)
|
|
||||||
|
|
||||||
assert hasattr(self, 'initial_data'), (
|
assert hasattr(self, 'initial_data'), (
|
||||||
'Cannot call `.is_valid()` as no `data=` keyword argument was '
|
'Cannot call `.is_valid()` as no `data=` keyword argument was '
|
||||||
'passed when instantiating the serializer instance.'
|
'passed when instantiating the serializer instance.'
|
||||||
|
@ -876,6 +861,7 @@ class ModelSerializer(Serializer):
|
||||||
models.DateField: DateField,
|
models.DateField: DateField,
|
||||||
models.DateTimeField: DateTimeField,
|
models.DateTimeField: DateTimeField,
|
||||||
models.DecimalField: DecimalField,
|
models.DecimalField: DecimalField,
|
||||||
|
models.DurationField: DurationField,
|
||||||
models.EmailField: EmailField,
|
models.EmailField: EmailField,
|
||||||
models.Field: ModelField,
|
models.Field: ModelField,
|
||||||
models.FileField: FileField,
|
models.FileField: FileField,
|
||||||
|
@ -890,11 +876,14 @@ class ModelSerializer(Serializer):
|
||||||
models.TextField: CharField,
|
models.TextField: CharField,
|
||||||
models.TimeField: TimeField,
|
models.TimeField: TimeField,
|
||||||
models.URLField: URLField,
|
models.URLField: URLField,
|
||||||
|
models.UUIDField: UUIDField,
|
||||||
models.GenericIPAddressField: IPAddressField,
|
models.GenericIPAddressField: IPAddressField,
|
||||||
models.FilePathField: FilePathField,
|
models.FilePathField: FilePathField,
|
||||||
}
|
}
|
||||||
if ModelDurationField is not None:
|
if postgres_fields:
|
||||||
serializer_field_mapping[ModelDurationField] = DurationField
|
serializer_field_mapping[postgres_fields.HStoreField] = HStoreField
|
||||||
|
serializer_field_mapping[postgres_fields.ArrayField] = ListField
|
||||||
|
serializer_field_mapping[postgres_fields.JSONField] = JSONField
|
||||||
serializer_related_field = PrimaryKeyRelatedField
|
serializer_related_field = PrimaryKeyRelatedField
|
||||||
serializer_related_to_field = SlugRelatedField
|
serializer_related_to_field = SlugRelatedField
|
||||||
serializer_url_field = HyperlinkedIdentityField
|
serializer_url_field = HyperlinkedIdentityField
|
||||||
|
@ -1585,19 +1574,6 @@ class ModelSerializer(Serializer):
|
||||||
return validators
|
return validators
|
||||||
|
|
||||||
|
|
||||||
if hasattr(models, 'UUIDField'):
|
|
||||||
ModelSerializer.serializer_field_mapping[models.UUIDField] = UUIDField
|
|
||||||
|
|
||||||
# IPAddressField is deprecated in Django
|
|
||||||
if hasattr(models, 'IPAddressField'):
|
|
||||||
ModelSerializer.serializer_field_mapping[models.IPAddressField] = IPAddressField
|
|
||||||
|
|
||||||
if postgres_fields:
|
|
||||||
ModelSerializer.serializer_field_mapping[postgres_fields.HStoreField] = HStoreField
|
|
||||||
ModelSerializer.serializer_field_mapping[postgres_fields.ArrayField] = ListField
|
|
||||||
ModelSerializer.serializer_field_mapping[postgres_fields.JSONField] = JSONField
|
|
||||||
|
|
||||||
|
|
||||||
class HyperlinkedModelSerializer(ModelSerializer):
|
class HyperlinkedModelSerializer(ModelSerializer):
|
||||||
"""
|
"""
|
||||||
A type of `ModelSerializer` that uses hyperlinked relationships instead
|
A type of `ModelSerializer` that uses hyperlinked relationships instead
|
||||||
|
|
Loading…
Reference in New Issue
Block a user