title: flask-marshmallow: how to mark all fields as optional only when the method is PUT
body: <p>I am making a flask restful api, what I'm having trouble with is  marshmallow-sqlalchemy, and webargs</p>
  
  <p>in short here is my sqlalchemy model:</p>
  
  <pre><code>class User(Model):
      id = Column(String, primary_key=True)
      name = Column(String(64), nullable=False)
      email = Column(String(120), nullable=False)
      password = Column(String(128))
      creation_date = Column(DateTime, default=datetime.utcnow)
  </code></pre>
  
  <p>and this is my schema</p>
  
  <pre><code>class UserSchema(ModelSchema):
      class Meta:
          model = User
          strict = True
          sqla_session = db.session
  
  user_schema = UserSchema()
  </code></pre>
  
  <p>and an example of my routes using flask-classful and webargs:</p>
  
  <pre><code>class UserView(FlaskView):
      trailing_slash = False
      model = User
      schema = user_schema
  
      @use_kwargs(schema.fields)
      def post(self, **kwargs):
          try:
              entity = self.model()
  
              for d in kwargs:
                  if kwargs[d] is not missing:
                      entity.__setattr__(d, kwargs[d])
  
              db.session.add(entity)
              db.session.commit()
              o = self.schema.dump(entity).data
              return jsonify({'{}'.format(self.model.__table__.name): o})
  
          except IntegrityError:
              return jsonify({'message': '{} exist in the database. choose another id'
                     .format(self.model.__table__.name)}), 409
  
  
      @use_kwargs(schema.fields)
      def put(self, id, **kwargs):
          entity = self.model.query.filter_by(id=id).first_or_404()
  
          for d in kwargs:
              if kwargs[d] is not missing:
                  entity.__setattr__(d, kwargs[d])
  
          db.session.commit()
          o = self.schema.dump(entity).data
          return jsonify({'{}'.format(self.model.__table__.name): o})
  
  UserView.register(app)
  </code></pre>
  
  <p><strong>The problem:</strong>
  as you can see in my sqlalchemy model, some fields are not nullable, thus my marshmallow schemda marks them as required. My <code>get</code>, <code>index</code>, <code>delete</code> and <code>post</code> methods all work perfectly. But I included post for one reason:</p>
  
  <p>when I try to post a new user with no name for example, a 422 http code is raised because <code>name</code> field is required, which is something I want and it is done perfectly. </p>
  
  <p>BUT when editing fields with <code>put</code> request, I wish EVERYTHING in my schema becomes optional.. right now if I wanted to update a user, I must provide not only the id.. but ALL other information required by default even if I didn't change them at all. </p>
  
  <p><strong>in short</strong>, how to mark all fields as "optional" when the method is "put"?</p>
  
  <p><strong><em>EDIT:</em></strong> just like the solution provided by @Mekicha, I made the following chnages:</p>
  
  <p>change the schema to make the required fields in my model accept the value None. like this:</p>
  
  <pre><code>class UserSchema(ModelSchema):
      class Meta:
          model = User
          ...
      name = fields.Str(missing=None, required=True)
      email = fields.Email(missing=None, required=True)
      ...
  </code></pre>
  
  <p>change my put and post method condition from this:</p>
  
  <pre><code>if kwargs[d] is not missing:
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>if kwargs[d] is not missing and kwargs[d] is not None:
  </code></pre>