Django REST Framework: integer fields default value not showing up in browseable API form

StackOverflow https://stackoverflow.com/questions/23228626

  •  07-07-2023
  •  | 
  •  

Question

Using django 1.6 and rest framework 2.3.13

In model class:

class A(models.Model):
    some_name = models.PositiveSmallIntegerField(default=15)

In serilizer:

class ASerializer(ModelSerializer):
    class Meta:
        model = A
        fields = (  'some_name'  )

In view:

class AViewSet(viewsets.ModelViewSet):

    queryset = A.objects.all()
    serializer_class = ASerializer

But in the api form, it's showing as 0, any idea?

Était-ce utile?

La solution

This problem can be solved by adding

if obj is None and self.default is not None:
    return self.default

to rest_framework/fields.py

Original pull request: https://github.com/tomchristie/django-rest-framework/pull/1248/files

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top