Storing Default Values for BooleanField() and IntegerField() in Django REST -


i know per documentation following fields not take allow_blank=true , allow_null=true

booleanfield() integerfield() 

i need allow client not specify g or d (as per below) , store value in db none.

g = serializers.booleanfield() d = serializers.integerfield() 

any ideas ?

the different options handling of empty, blank , null fields (necessarily) little subtle, unsurprising trips folks up.

i know per documentation following fields not take allow_blank=true , allow_null=true

that's incorrect:

  • integerfield(allow_null=true) valid.
  • if want allow null inputs boolean fields need use nullbooleanfield() class.

you correct neither of them take allow_blank, empty string isn't going valid value in either case.


i need allow client not specify g or d (as per below) , store value in db none.

you can either use integerfield(default=none) , nullbooleanfield(default=none).

in case when values omitted included none in serializer.validated_data. you'll want make sure use null=true/nullbooleanfield on model field.

or integerfield(required=false) , nullbooleanfield(required=false).

in case when values omitted not included in serializer.validated_data, , model field default used. you'll want make sure use default=none , null=true/nullbooleanfield on model field.


note there bug when using browsable api empty fields in html input did not default values set. resolved in the upcoming 3.1.4 release.

the initial argument suggested in edwin's answer can useful, setting value rendered in html form fields.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -