Last active
September 21, 2022 17:58
-
-
Save namper/198b93a3b18c3edef5d8ccaafac306dd to your computer and use it in GitHub Desktop.
DRF Switch IO Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SwitchIOField(Field): | |
def __init__( | |
self, | |
input_field: Field, | |
output_field: Field, | |
*args, **kwargs | |
): | |
# -- setting up input field | |
self.input_field = input_field | |
self.validators = input_field.validators | |
self.input_field.read_only = True | |
# -- setting up output field | |
self.output_field = output_field | |
self.output_field.write_only = True | |
# -- copy field data | |
self.output_field.required = kwargs.get('required', False) | |
self.output_field.allow_null = kwargs.get('allow_null', False) | |
self.label = self.output_field.label or kwargs.get('label', None) | |
self.source = self.output_field.source or kwargs.get('source', None) | |
super().__init__(*args, **kwargs) | |
def validate_empty_values(self, data): | |
return self.output_field.validate_empty_values(data) | |
def to_internal_value(self, data): | |
return self.input_field.to_internal_value(data) | |
def to_representation(self, value): | |
return self.output_field.to_internal_value(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment