python 3.x - django rest framwork iterate throug fields in model serializer -


i want iterate through fields in modelserializer , want make field required . not working . how can that. please me.

class customerserializer(serializers.modelserializer):      class meta:         model = customer          fields = ("email", "phone_no", "full_name", "landline_no")      def __init__(self, *args, **kwargs):         super().__init__(*args, **kwargs)          field in self.fields:             self.fields[field].required = true             self.fields[field].allow_blank = false 

you can add args in meta class modelserializer, this:

class meta:     model = customer      fields = ("email", "phone_no", "full_name", "landline_no")     extra_kwargs = {'email': {'required': true, 'allow_blank': false}} 

if need fields, should reconsider fields inside customer model. can add blank=false and/or null=false. rest framework takes information consideration when creating modelserializer


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -