Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming error with WTForms StringField writing to Postgres

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?

like image 505
KillerSnail Avatar asked Jul 19 '26 13:07

KillerSnail


1 Answers

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()
like image 192
Doobeh Avatar answered Jul 22 '26 09:07

Doobeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!