Python rest_framework.fields.IntegerField() Examples

The following are 7 code examples of rest_framework.fields.IntegerField(). 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_filtering.py    From drf-mongo-filters with GNU General Public License v2.0 6 votes vote down vote up
def test_range(self):
        objects = [
            SimpleDoc.objects.create(f_int=3),
            SimpleDoc.objects.create(f_int=5),
            SimpleDoc.objects.create(f_int=7),
            SimpleDoc.objects.create(f_int=11),
            SimpleDoc.objects.create(f_int=13),
        ]

        class FS(Filterset):
            foo = filters.RangeFilter(child=fields.IntegerField(), source='f_int')
            bar = filters.RangeFilter(child=fields.IntegerField(), lookup=('gt','lt'), source='f_int')

        fs = FS({'foo': {'min':5, 'max':11}})
        qs = fs.filter_queryset(SimpleDoc.objects.all())
        self.assertQuerysetDocs(qs, objects[1:4])

        fs = FS({'bar': {'min':5, 'max':11}})
        qs = fs.filter_queryset(SimpleDoc.objects.all())
        self.assertQuerysetDocs(qs, objects[2:3]) 
Example #2
Source File: test_filtering.py    From drf-mongo-filters with GNU General Public License v2.0 6 votes vote down vote up
def test_range_intersect(self):
        objects = [
            SimpleDoc.objects.create(f_rng_beg=1, f_rng_end=3),
            SimpleDoc.objects.create(f_rng_beg=2, f_rng_end=4), # 4-6
            SimpleDoc.objects.create(f_rng_beg=3, f_rng_end=5), # 4-6
            SimpleDoc.objects.create(f_rng_beg=4, f_rng_end=6), # 4-6
            SimpleDoc.objects.create(f_rng_beg=5, f_rng_end=7), # 4-6
            SimpleDoc.objects.create(f_rng_beg=6, f_rng_end=8), # 4-6
            SimpleDoc.objects.create(f_rng_beg=7, f_rng_end=9)
        ]

        class FS(Filterset):
            foo = filters.IntersectRangeFilter(('f_rng_beg','f_rng_end'), child=fields.IntegerField())

        fs = FS({'foo': {'min':4, 'max':6}})
        qs = fs.filter_queryset(SimpleDoc.objects.all())
        self.assertQuerysetDocs(qs, objects[1:-1]) 
Example #3
Source File: entities.py    From drf_openapi with MIT License 6 votes vote down vote up
def get_paginator_serializer(self, view, child_serializer_class):
        class BaseFakeListSerializer(serializers.Serializer):
            results = child_serializer_class(many=True)

        class FakePrevNextListSerializer(BaseFakeListSerializer):
            next = URLField()
            previous = URLField()

        # Validate if the view has a pagination_class
        if not (hasattr(view, 'pagination_class')) or view.pagination_class is None:
            return BaseFakeListSerializer

        pager = view.pagination_class
        if hasattr(pager, 'default_pager'):
            # Must be a ProxyPagination
            pager = pager.default_pager

        if issubclass(pager, (PageNumberPagination, LimitOffsetPagination)):
            class FakeListSerializer(FakePrevNextListSerializer):
                count = IntegerField()
            return FakeListSerializer
        elif issubclass(pager, CursorPagination):
            return FakePrevNextListSerializer

        return BaseFakeListSerializer 
Example #4
Source File: test_reference.py    From django-rest-framework-mongoengine with MIT License 6 votes vote down vote up
def test_custom_nested(self):
        class CustomReferencing(Serializer):
            foo = IntegerField()

        class TestSerializer(DocumentSerializer):
            serializer_reference_nested = CustomReferencing

            class Meta:
                model = ReferencingDoc
                fields = '__all__'
                depth = 1

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                ref = NestedSerializer(read_only=True):
                    foo = IntegerField()
        """)
        assert repr(TestSerializer()) == expected 
Example #5
Source File: test_dumb.py    From django-rest-framework-mongoengine with MIT License 5 votes vote down vote up
def test_mapping(self):
        class TestSerializer(DocumentSerializer):
            class Meta:
                model = DumbDocument
                fields = '__all__'

        expected = dedent("""
            TestSerializer():
                id = ObjectIdField(read_only=True)
                name = CharField(required=False)
                foo = IntegerField(required=False)
        """)

        # better output then self.assertEqual()
        assert repr(TestSerializer()) == expected 
Example #6
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 #7
Source File: test_serializers.py    From drf-haystack with MIT License 4 votes vote down vote up
def setUp(self):
        MockPersonIndex().reindex()
        MockPetIndex().reindex()

        class Serializer1(HaystackSerializer):

            integer_field = serializers.IntegerField()
            city = serializers.SerializerMethodField()

            class Meta:
                index_classes = [MockPersonIndex]
                fields = ["text", "firstname", "lastname", "autocomplete"]

            def get_integer_field(self, instance):
                return 1

            def get_city(self, instance):
                return "Declared overriding field"

        class Serializer2(HaystackSerializer):

            class Meta:
                index_classes = [MockPersonIndex]
                exclude = ["firstname"]

        class Serializer3(HaystackSerializer):

            class Meta:
                index_classes = [MockPersonIndex]
                fields = ["text", "firstname", "lastname", "autocomplete"]
                ignore_fields = ["autocomplete"]

        class Serializer7(HaystackSerializer):

            class Meta:
                index_classes = [MockPetIndex]

        class ViewSet1(HaystackViewSet):
            serializer_class = Serializer1

            class Meta:
                index_models = [MockPerson]

        self.serializer1 = Serializer1
        self.serializer2 = Serializer2
        self.serializer3 = Serializer3
        self.serializer7 = Serializer7
        self.view1 = ViewSet1