Python rest_framework.fields.CharField() Examples

The following are 18 code examples of rest_framework.fields.CharField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rest_framework.fields , or try the search function .
Example #1
Source File: test_dynamic.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_extended(self):
        class TestSerializer(DynamicDocumentSerializer):
            bar = drf_fields.CharField(required=False)

            class Meta:
                model = DumbDynamic
                fields = '__all__'

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                bar = CharField(required=False)
                name = CharField(required=False)
                foo = IntegerField(required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #2
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_embedding(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = NestedEmbeddingDoc
                fields = '__all__'
                depth = 1

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                    embedded = EmbeddedSerializer(required=False):
                        name = CharField(required=False)
                        foo = IntegerField(required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #3
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_embedding_nodepth(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = NestedEmbeddingDoc
                fields = '__all__'
                depth = 0

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                    embedded = EmbeddedSerializer(required=False):
                        name = CharField(required=False)
                        foo = IntegerField(required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #4
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_embedding_custom_bottom(self):
        class CustomEmbedding(Field):
            bla = drf_fields.CharField()

        class TestSerializer(DocumentSerializer):
            serializer_embedded_bottom = CustomEmbedding

            class Meta:
                model = NestedEmbeddingDoc
                fields = '__all__'
                depth_embedding = 0

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded = CustomEmbedding(default=None, required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #5
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_embedding_recursive(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = RecursiveEmbeddingDoc
                fields = '__all__'

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                    embedded = EmbeddedSerializer(required=False):
                        name = CharField(required=False)
                        embedded = EmbeddedSerializer(required=False):
                            name = CharField(required=False)
                            embedded = EmbeddedSerializer(required=False):
                                name = CharField(required=False)
                                embedded = EmbeddedSerializer(required=False):
                                    name = CharField(required=False)
                                    embedded = HiddenField(default=None, required=False)
        """)

        serializer = TestSerializer()
        assert repr(serializer) == expected 
Example #6
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_embedding_recursive_restricted(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = RecursiveEmbeddingDoc
                fields = '__all__'
                depth_embedding = 2

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                    embedded = EmbeddedSerializer(required=False):
                        name = CharField(required=False)
                        embedded = HiddenField(default=None, required=False)
        """)

        serializer = TestSerializer()
        assert repr(serializer) == expected 
Example #7
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_embedding_custom_nested(self):
        class CustomTestSerializer(Serializer):
            bla = drf_fields.CharField()

        class TestSerializer(DocumentSerializer):
            serializer_embedded_nested = CustomTestSerializer

            class Meta:
                model = NestedEmbeddingDoc
                fields = '__all__'

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded = EmbeddedSerializer(required=False):
                    bla = CharField()
        """)
        assert repr(TestSerializer()) == expected 
Example #8
Source File: test_serializers.py    From drf-haystack with MIT License 5 votes vote down vote up
def test_serializer_get_fields_with_ignore_fields(self):
        obj = SearchQuerySet().filter(lastname="Foreman")[0]
        serializer = self.serializer3(instance=obj)
        fields = serializer.get_fields()

        self.assertIsInstance(fields, dict)
        self.assertIsInstance(fields["text"], CharField)
        self.assertIsInstance(fields["firstname"], CharField)
        self.assertIsInstance(fields["lastname"], CharField)
        self.assertFalse("autocomplete" in fields) 
Example #9
Source File: test_serializers.py    From drf-haystack with MIT License 5 votes vote down vote up
def test_serializer_get_fields_with_exclude(self):
        obj = SearchQuerySet().filter(lastname="Foreman")[0]
        serializer = self.serializer2(instance=obj)
        fields = serializer.get_fields()

        self.assertIsInstance(fields, dict)
        self.assertIsInstance(fields["text"], CharField)
        self.assertIsInstance(fields["lastname"], CharField)
        self.assertIsInstance(fields["autocomplete"], CharField)
        self.assertFalse("firstname" in fields) 
Example #10
Source File: test_serializers.py    From drf-haystack with MIT License 5 votes vote down vote up
def test_serializer_get_fields(self):
        obj = SearchQuerySet().filter(lastname="Foreman")[0]
        serializer = self.serializer1(instance=obj)
        fields = serializer.get_fields()

        self.assertIsInstance(fields, dict)
        self.assertIsInstance(fields["integer_field"], IntegerField)
        self.assertIsInstance(fields["text"], CharField)
        self.assertIsInstance(fields["firstname"], CharField)
        self.assertIsInstance(fields["lastname"], CharField)
        self.assertIsInstance(fields["autocomplete"], CharField) 
Example #11
Source File: fields.py    From drf-haystack with MIT License 5 votes vote down vote up
def bind(self, field_name, parent):
        """
        Initializes the field name and parent for the field instance.
        Called when a field is added to the parent serializer instance.
        Taken from DRF and modified to support drf_haystack multiple index
        functionality.
        """

        # In order to enforce a consistent style, we error if a redundant
        # 'source' argument has been used. For example:
        # my_field = serializer.CharField(source='my_field')
        assert self.source != field_name, (
            "It is redundant to specify `source='%s'` on field '%s' in "
            "serializer '%s', because it is the same as the field name. "
            "Remove the `source` keyword argument." %
            (field_name, self.__class__.__name__, parent.__class__.__name__)
        )

        self.field_name = field_name
        self.parent = parent

        # `self.label` should default to being based on the field name.
        if self.label is None:
            self.label = field_name.replace('_', ' ').capitalize()

        # self.source should default to being the same as the field name.
        if self.source is None:
            self.source = self.convert_field_name(field_name)

        # self.source_attrs is a list of attributes that need to be looked up
        # when serializing the instance, or populating the validated data.
        if self.source == '*':
            self.source_attrs = []
        else:
            self.source_attrs = self.source.split('.') 
Example #12
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_embedding_required(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = RequiredEmbeddingDoc
                fields = '__all__'

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded = EmbeddedSerializer(required=True):
                    name = CharField(required=False)
                    foo = IntegerField(required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #13
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_embedding_list(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = ListEmbeddingDoc
                fields = '__all__'

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded_list = EmbeddedSerializer(many=True, required=False):
                    name = CharField(required=False)
                    foo = IntegerField(required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #14
Source File: test_embedded.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_embedding_restricted(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = NestedEmbeddingDoc
                fields = '__all__'
                depth_embedding = 1

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                embedded = EmbeddedSerializer(required=False):
                    name = CharField(required=False)
                    embedded = HiddenField(default=None, required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #15
Source File: test_dynamic.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_repr(self):
        expected = dedent("""
            DocumentEmbeddingDynamicSerializer():
                name = CharField(required=False)
                foo = IntegerField(required=False)
                embedded = DumbDynamicEmbeddedSerializer():
                    name = CharField(required=False)
                    foo = IntegerField(required=False)
        """)
        assert repr(DocumentEmbeddingDynamicSerializer()) == expected 
Example #16
Source File: test_dynamic.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_repr(self):
        expected = dedent("""
            EmbeddingDynamicSerializer():
                name = CharField(required=False)
                foo = IntegerField(required=False)
                embedded = DumbEmbeddedSerializer():
                    name = CharField(required=False)
                    foo = IntegerField(required=False)
        """)
        assert repr(EmbeddingDynamicSerializer()) == expected 
Example #17
Source File: test_dynamic.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_declared(self):
        class TestSerializer(DynamicDocumentSerializer):
            class Meta:
                model = DumbDynamic
                fields = '__all__'

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                name = CharField(required=False)
                foo = IntegerField(required=False)
        """)
        assert repr(TestSerializer()) == expected 
Example #18
Source File: serializers.py    From django-rest-framework-mongoengine with MIT License 4 votes vote down vote up
def build_standard_field(self, field_name, model_field):
        field_mapping = ClassLookupDict(self.serializer_field_mapping)

        field_class = field_mapping[model_field]
        field_kwargs = get_field_kwargs(field_name, model_field)

        if 'choices' in field_kwargs:
            # Fields with choices get coerced into `ChoiceField`
            # instead of using their regular typed field.
            field_class = self.serializer_choice_field
            # Some model fields may introduce kwargs that would not be valid
            # for the choice field. We need to strip these out.
            # Eg. models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES)
            valid_kwargs = set((
                'read_only', 'write_only',
                'required', 'default', 'initial', 'source',
                'label', 'help_text', 'style',
                'error_messages', 'validators', 'allow_null', 'allow_blank',
                'choices'
            ))
            for key in list(field_kwargs.keys()):
                if key not in valid_kwargs:
                    field_kwargs.pop(key)

        if 'regex' in field_kwargs:
            field_class = drf_fields.RegexField

        if not issubclass(field_class, drfm_fields.DocumentField):
            # `model_field` is only valid for the fallback case of
            # `ModelField`, which is used when no other typed field
            # matched to the model field.
            field_kwargs.pop('model_field', None)

        if not issubclass(field_class, drf_fields.CharField) and not issubclass(field_class, drf_fields.ChoiceField):
            # `allow_blank` is only valid for textual fields.
            field_kwargs.pop('allow_blank', None)

        if field_class is drf_fields.BooleanField and field_kwargs.get('allow_null', False):
            field_kwargs.pop('allow_null', None)
            field_kwargs.pop('default', None)
            field_class = drf_fields.NullBooleanField

        return field_class, field_kwargs