def __init__(self, model_field, **kwargs): self.model_field = model_field # The `max_length` option is supported by Django's base `Field` class, # so we'd better support it here. max_length = kwargs.pop('max_length', None) super(ModelField, self).__init__(**kwargs) if max_length is not None: message = self.error_messages['max_length'].format(max_length=max_length) self.validators.append(MaxLengthValidator(max_length, message=message))
class SnippetSerializer(serializers.Serializer): """SnippetSerializer classdoc create: docstring for create from serializer classdoc """ id = serializers.IntegerField(read_only=True, help_text="id serializer help text") created = UnixTimestampField(read_only=True) owner = serializers.PrimaryKeyRelatedField( queryset=get_user_model().objects.all(), default=serializers.CurrentUserDefault(), help_text="The ID of the user that created this snippet; if none is provided, " "defaults to the currently logged in user." ) owner_as_string = serializers.PrimaryKeyRelatedField( help_text="The ID of the user that created this snippet.", pk_field=serializers.CharField(help_text="this help text should not show up"), read_only=True, source='owner', ) title = serializers.CharField(required=False, allow_blank=True, max_length=100) code = serializers.CharField(style={'base_template': 'textarea.html'}) tags = serializers.ListField(child=serializers.CharField(min_length=2), min_length=3, max_length=15) linenos = serializers.BooleanField(required=False) language = LanguageSerializer(help_text="Sample help text for language") styles = serializers.MultipleChoiceField(choices=STYLE_CHOICES, default=['solarized-dark']) lines = serializers.ListField(child=serializers.IntegerField(), allow_empty=True, allow_null=True, required=False) example_projects = serializers.ListSerializer(child=ExampleProjectSerializer(), read_only=True, validators=[MaxLengthValidator(100)]) difficulty_factor = serializers.FloatField(help_text="this is here just to test FloatField", read_only=True, default=lambda: 6.9) rate_as_string = serializers.DecimalField(max_digits=6, decimal_places=3, default=Decimal('0.0'), validators=[MinValueValidator(Decimal('0.0'))]) rate = serializers.DecimalField(max_digits=6, decimal_places=3, default=Decimal('0.0'), coerce_to_string=False, validators=[MinValueValidator(Decimal('0.0'))]) def create(self, validated_data): """ Create and return a new `Snippet` instance, given the validated data. """ del validated_data['styles'] del validated_data['lines'] del validated_data['difficulty_factor'] return Snippet.objects.create(**validated_data) def update(self, instance, validated_data): """ Update and return an existing `Snippet` instance, given the validated data. """ instance.title = validated_data.get('title', instance.title) instance.code = validated_data.get('code', instance.code) instance.linenos = validated_data.get('linenos', instance.linenos) instance.language = validated_data.get('language', instance.language) instance.style = validated_data.get('style', instance.style) instance.save() return instance
def __init__(self, **kwargs): self.allow_blank = kwargs.pop('allow_blank', False) max_length = kwargs.pop('max_length', None) min_length = kwargs.pop('min_length', None) super(CharField, self).__init__(**kwargs) if max_length is not None: message = self.error_messages['max_length'].format(max_length=max_length) self.validators.append(MaxLengthValidator(max_length, message=message)) if min_length is not None: message = self.error_messages['min_length'].format(min_length=min_length) self.validators.append(MinLengthValidator(min_length, message=message))
class Meta: model = Trigger fields = ('id', 'label', 'description', 'created_at', 'updated_at', 'codebox', 'signals', 'event') extra_kwargs = { 'signals': { 'validators': [ MaxLengthValidator( 8, 'Ensure this value has at most %(limit_value)d elements (it has %(show_value)d).' ) ] } }
def __init__(self, max_length=14, min_length=11, **kwargs): self.allow_blank = kwargs.get('allow_blank', False) self.max_length = kwargs.get('max_length', None) self.min_length = kwargs.get('min_length', None) super(BRCPFField, self).__init__(**kwargs) if self.max_length is not None: message = self.error_messages['max_length'].format( max_length=self.max_length) self.validators.append( MaxLengthValidator(self.max_length, message=message)) if self.min_length is not None: message = self.error_messages['min_length'].format( min_length=self.min_length) self.validators.append( MinLengthValidator(self.min_length, message=message))
def __init__(self, **kwargs): self.allow_blank = kwargs.pop('allow_blank', False) self.trim_whitespace = kwargs.pop('trim_whitespace', True) self.max_length = kwargs.pop('max_length', None) self.min_length = kwargs.pop('min_length', None) self.split_flag = kwargs.pop('split_flag', True) super(IdsSplitField, self).__init__(**kwargs) if self.max_length is not None: message = lazy(self.error_messages['max_length'].format, six.text_type)(max_length=self.max_length) self.validators.append( MaxLengthValidator(self.max_length, message=message)) if self.min_length is not None: message = lazy(self.error_messages['min_length'].format, six.text_type)(min_length=self.min_length) self.validators.append( MinLengthValidator(self.min_length, message=message))