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

Integer array field in postgress is not handled by flask admin properly #1724

Open
remort opened this issue Oct 12, 2018 · 6 comments
Open

Comments

@remort
Copy link

remort commented Oct 12, 2018

Integer array field in my table in postgres exists and work perfectly. I can create/apdate/insert/delete it with numbers.
Flask-admin sees and interprets that field by SQLAlchemy model. Field shows up with its content in edit form in flask-admin.
But whenever I try to save the form (regardless of editing or leaving contents of array field as is) I got error 500. Postgres throws an error:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column "courses_ids" is of type integer[] but expression is of type text[]
So it seems that flask-admin interprets my int array field as text array.
Is there any solution on how to instruct Flask-Admin that the array field is of type int ?

Sqlalchemy field declaration: courses_ids = Column(ARRAY(Integer))

@aaron-nall
Copy link

A workaround:

class Select2IntegerTagsField(Select2TagsField):
    def process_formdata(self, valuelist):
        super().process_formdata(valuelist)
        self.data = [int(x) for x in self.data]


class StudentView(ModelView):
    def scaffold_form(self):
        form_class = super().scaffold_form()
        form_class.course_ids = Select2IntegerTagsField("Course Ids", save_as_list=True)
        return form_class

admin.add_view(StudentView(Student, session))

@antonyc
Copy link

antonyc commented Jul 16, 2019

Hi. Will this be systematically fixed without workarounds?

@chen-zhuohan
Copy link

thanks! it's help me a lot

@Shir-AH
Copy link

Shir-AH commented Jun 27, 2020

A workaround:

class Select2IntegerTagsField(Select2TagsField):
    def process_formdata(self, valuelist):
        super().process_formdata(valuelist)
        self.data = [int(x) for x in self.data]


class StudentView(ModelView):
    def scaffold_form(self):
        form_class = super().scaffold_form()
        form_class.course_ids = Select2IntegerTagsField("Course Ids", save_as_list=True)
        return form_class

admin.add_view(StudentView(Student, session))

what is the Select2TagsField Class you're inheriting from?

@catalintrif
Copy link

Please review this fix #2003

@ovyan
Copy link

ovyan commented Aug 8, 2021

For future generations.

what is the Select2TagsField Class you're inheriting from?

from flask_admin.form.fields import Select2TagsField

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

7 participants