title: Flask SqlAlchemy AttributeError: 'str' object has no attribute '_sa_instance_state'
body: <p>So, I am trying to add images name that I save in specified directory, but this error keeps coming and nothing is been added in the database, Although the images keep getting saved in the specified directory.
  Here are all my files
  <strong><em>Models.py</em></strong></p>
  
  <pre><code>from shop import db
  from datetime import datetime
  
  
  class Product(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(80), nullable=False)
      price = db.Column(db.Numeric(10,2), nullable=False)
      stock = db.Column(db.Integer, nullable=False)
      desc = db.Column(db.Text, nullable=False)
      pub_date = db.Column(db.DateTime, nullable=False,
          default=datetime.utcnow)
  
      brand_id = db.Column(db.Integer, db.ForeignKey('brand.id'),
          nullable=False)
      brand = db.relationship('Brand',
          backref=db.backref('brands', lazy=True))
  
      category_id = db.Column(db.Integer, db.ForeignKey('category.id'),
          nullable=False)
      category = db.relationship('Category',
          backref=db.backref('categories', lazy=True))
  
      image_1 = db.Column(db.String(256), nullable=False, default='image1.jpg')
      image_2 = db.Column(db.String(256), nullable=False, default='image2.jpg')
      image_3 = db.Column(db.String(256), nullable=False, default='image3.jpg')
  
      def __repr__(self):
          return '&lt;Product %r&gt;' % self.name
  
  class Brand(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(64), nullable=False, unique=True)
  
  class Category(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(64), nullable=False, unique=True)
  
  db.create_all()
  </code></pre>
  
  <p><strong><em>forms.py</em></strong></p>
  
  <pre><code>from flask_wtf.file import FileAllowed, FileField, FileRequired
  from wtforms import Form,StringField, IntegerField, BooleanField, TextAreaField, validators
  
  class AddProducts(Form):
      name = StringField("Name", [validators.DataRequired()])
      price = IntegerField("Price:RS ", [validators.DataRequired()])
      stock = IntegerField("Stock", [validators.DataRequired()])
      desc = TextAreaField("Description", [validators.DataRequired()])
      # colors = TextAreaField("Colors", [validators.DataRequired()])
  
      image_1 = FileField('Image 1', [FileRequired(), FileAllowed(['jpg, jpeg, png, svg, gif']), "Images Only please"])
      image_2 = FileField('Image 2', [FileRequired(), FileAllowed(['jpg, jpeg, png, svg, gif']), "Images Only please"])
      image_3 = FileField('Image 3', [FileRequired(), FileAllowed(['jpg, jpeg, png, svg, gif']), "Images Only please"]) 
  
  </code></pre>
  
  <p><strong>*routes.py</strong></p>
  
  <pre><code>
  @app.route('/addproduct', methods=["GET", "POST"])
  def addproduct():
      brands = Brand.query.all()
      categories = Category.query.all()
      form = AddProducts(request.form)
      if request.method == "POST":
          name = form.name.data
          price = form.price.data
          stock = form.stock.data
          desc = form.desc.data
          brand = request.form.get('brand')
          category = request.form.get('category')
          image_1 = photos.save(request.files['image_1'] , name=secrets.token_hex(10) + '.')
          image_2 = photos.save(request.files['image_2'] , name=secrets.token_hex(10) + '.')
          image_3 = photos.save(request.files['image_3'] , name=secrets.token_hex(10) + '.')
          print(f"Image 1 name:{image_1}, its type:{type(image_1)}")
          product = Product(name=name, price=price, stock=stock, desc=desc, brand=brand, category=category, 
                                  image_1=image_1,image_2=image_2, image_3=image_3)
          db.session.add(product)
          flash(f"{name} has been added to database.", 'success')
          db.session.commit()
          return redirect(url_for('admin'))
      return render_template('products/addproduct.html', title='Add Product', form=form, brands=brands, 
                              categories=categories)
  </code></pre>
  
  <p>All the images type are strings, and model fields are string too, still I keep getting this error.
  Here is my html page for this form</p>
  
  <pre><code>{% extends 'layout.html' %}
  
  {% block body_block %}
  {% include '_messages.html' %}
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
      &lt;div class="col-md-2"&gt;&lt;/div&gt;
      &lt;div class="col-md-8"&gt;
          &lt;h2 class="text-center bg-info p-2"&gt;
              Add product
          &lt;/h2&gt;
          {% from '_formhelpers.html' import render_field %}
          &lt;form action="" method="POST" enctype="multipart/form-data"&gt;
              {{ render_field(form.name, class="form-control", placeholder="Product Name")}}
              {{ render_field(form.price, class="form-control", placeholder="Price") }}
              {{ render_field(form.stock, class="form-control", placeholder="Stock") }}
              &lt;label for="brand"&gt;Add a Brand&lt;/label&gt;
              &lt;select class="form-control" name="brand" id="brand"&gt;
                  &lt;option value="" class="form-control" required&gt; Select a Brand&lt;/option&gt;
                  {% for brand in brands%}
                      &lt;option value="brand.id" class="form-control"&gt;{{brand.name}}&lt;/option&gt;
                  {% endfor %}
              &lt;/select&gt;
              &lt;label for="category"&gt;Add a Category&lt;/label&gt;
              &lt;select class="form-control" name="category" id="category"&gt;
                  &lt;option value="" class="form-control" required&gt; Select a Category&lt;/option&gt;
                  {% for category in categories %}
                      &lt;option value="category.id" class="form-control"&gt;{{category.name}}&lt;/option&gt;
                  {% endfor %}
              &lt;/select&gt;
              {{ render_field(form.desc, class="form-control", placeholder="Product Description", rows=10) }}
              &lt;div class="container"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-md-4"&gt;
                          {{ render_field(form.image_1, class="form-control")}}
                      &lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                          {{ render_field(form.image_2, class="form-control")}}
                      &lt;/div&gt;
                      &lt;div class="col-md-4"&gt;
                          {{ render_field(form.image_3, class="form-control")}}
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;button type="submit" class="btn btn-outline-info mt-4"&gt;Add Product&lt;/button&gt;
          &lt;/form&gt;
      &lt;/div&gt;
      &lt;div class="col-md-2"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  {% endblock body_block %}
  </code></pre>