My database is a Postgres instance. My model is defined as:
from app import db
class Device(db.Model):
__tablename__ = 'device'
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String())
my from is defined as:
from flask_wtf import FlaskForm
from wtforms import StringField
class EditDevices(FlaskForm):
device_name = StringField("Device Name", validators=[])
submit = SubmitField['Submit']
I get an error in the form that says:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'StringField' [SQL: 'UPDATE device SET name=%(name)s WHERE device.id = %(device_id)s'] [parameters: {'name': <wtforms.fields.core.StringField object at 0x110580c18>, 'device_id': 208}]
any ideas where I am going wrong?
Your code is incomplete-- you're not showing where you actually try to set the model data and save it to the database, but the error says <wtforms.fields.core.StringField object at 0x110580c18> is the value you're setting to the name attribute, so you're likely doing:
device = Device()
device.name = form.device_name
db.session.add(device)
db.session.commit()
Which would lead to something similar to the error you're seeing because form.device_name is a StringField whereas you want the user-submitted data. To get the data from the form element you need to use the .data property:
device = Device()
device.name = form.device_name.data
db.session.add(device)
db.session.commit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With