-1

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

1 Answer 1

0

It's obviously. The reviewer is a string defined in Project. You can define a __repr__ function in User model to return a string(name of user or something else). Check this question.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but it still not work and return InterfaceError: <unprintable InterfaceError object> this time.
Try define __str__ function in User model.
Yes, I was using __str__ and then switch to __repr__ but both did not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.