Created
September 1, 2018 01:19
-
-
Save rkdgusrnrlrl/05bc2a528e6ea4974be013f0f5266ec7 to your computer and use it in GitHub Desktop.
choice 값 검증 필드(이미 django 에 구현 돼 있지만, 기록을 위해 남겨둠)
This file contains hidden or 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
| from django.db import models | |
| class ChoicesValidator: | |
| def __init__(self, choices): | |
| self.choices = choices | |
| def __call__(self, value): | |
| choice_values = [t[0] for t in self.choices] | |
| if value not in choice_values: | |
| from django.core.exceptions import ValidationError | |
| raise ValidationError( | |
| 'API Method "{}" is not one of the permitted values: {}'.format( | |
| value, | |
| ', '.join(choice_values))) | |
| class SelectableCharField(models.CharField): | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| self.validators.append(ChoicesValidator(self.choices)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment