Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why QuerySelectField does not work in custom edit view? #1958

Open
Harry2049 opened this issue Feb 26, 2020 · 1 comment
Open

Why QuerySelectField does not work in custom edit view? #1958

Harry2049 opened this issue Feb 26, 2020 · 1 comment

Comments

@Harry2049
Copy link

I have the following problem: on stack Flask, Sqlalchemy, Flask-Admin created the following models:

class Store(db.Model):
id = db.Column(db.Integer, primary_key=True)
address = db.Column(db.String(200))

users = db.relationship('User', backref='store', lazy='dynamic')

class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
store_id = db.Column(db.Integer, db.ForeignKey('store.id'))
name = db.Column(db.String(128))
login = db.Column(db.String(20))
password = db.Column(db.String(20))

I use the following forms and views:

class UserForm(FlaskForm):
store_id = QuerySelectField('Склад', query_factory=lambda: Store.query)
name = StringField('Name')
login = StringField('Login')
password = StringField('Password')

class AdminSet(AdminModelView):
def edit_form(self, obj=None):
form = UserForm(obj=obj)
return form

The view works without problems, but when I try to save the changes, the following error appears:

Failed to update record. (psycopg2.ProgrammingError) can't adapt type 'Store' [SQL: UPDATE "user" SET store_id=%(store_id)s WHERE "user".id = %(user_id)s] [parameters: {'store_id': <Store 2>, 'user_id': 2}] (Background on this error at: http://sqlalche.me/e/f405)

Why is this happening and what am I doing wrong?

@mlenzen
Copy link

mlenzen commented Mar 6, 2020

QuerySelectField returns the actual object which you are then assigning to the id column where it expects an integer. Add a store = db.relationship(Store) to the model then change store_id = ... to store = ... in the form and I think it will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants