I was trying to save a project's reviewer using below, and the select field shows correct:
# Query the user with Role.id == 4 as reviewer
def reviewer_choices():
return User.query.join(User.roles).filter(Role.id == 4)
# Build a select field
class ProjectView(sqla.ModelView):
form_extra_fields = {
'reviewer': sqla.fields.QuerySelectField(
label='Reviewer',
query_factory=reviewer_choices,
)}
However, when I was trying to save it, the error occurred:
InterfaceError: (sqlite3.InterfaceError) Error binding parameter 8 - probably unsupported type. [SQL: u'INSERT INTO project(...reviewer...)VALUES(...<__main__.User object at 0x00000000048E89E8>...)
.
And I noticed that the reviewer is an object, something like: <__main__.User object at 0x00000000048E89E8>
. So what is the correct data type of reviewer so I can save it into database? I currently used:
In the project class
class Project(db.Model):
# ...
reviewer = db.Column(db.Unicode(128))
# ...
In the project table
CREATE TABLE `project` (
# ...
`reviewer1` TEXT,
# ...
And I also tried to define __repr__
and __str__
but both not worked:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(255))
# ...
# ...
def __repr__(self):
return self.first_name
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(255))
# ...
# ...
def __str__(self):
return self.first_name
InterfaceError: <unprintable InterfaceError object>
this time. – Parenthesis