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
Post a Comment