python - how to export column with relationship in flask-admin -
i have problem in export csv tables relationship others, while in 'simple' work well. have add basis export? example, db.model:
class categoria(db.model): __tablename__ = 'categorie' id = db.column(db.integer, primary_key=true, autoincrement=true) categoria = db.column(db.string(30), primary_key=true) tipo_id = db.column(db.integer, db.foreignkey('tipi.id'), primary_key=true) tipo = db.relationship('tipo', backref='categorie')
and modelview
class categorieadmin(sqla.modelview): column_display_pk = true can_export = true export_types = ['xls'] list_columns = ['categoria', 'tipo']
the error generate is: exception: unexpected data type <class '__main__.tipo'>
thanks help
the question quite old had same problem , resolved column_formatters_export.
column_formatters_export attribute can assigned dictionary keys name of columns of model, , values assigned function adds functionality change format or need.
for example code:
class categorieadmin(sqla.modelview): column_display_pk = true can_export = true export_types = ['xls'] list_columns = ['categoria', 'tipo'] column_formatters_export = dict( categoria=lambda v, c, m, p: '' if m.tipo none else m.tipo.categoria )
in m have model , can column of model. other possible solution add representation method model.
for example:
class categoria(db.model): __tablename__ = 'categorie' id = db.column(db.integer, primary_key=true, autoincrement=true) categoria = db.column(db.string(30), primary_key=true) tipo_id = db.column(db.integer, db.foreignkey('tipi.id'), primary_key=true) tipo = db.relationship('tipo', backref='categorie') def __repr__(self): return '%s' % self.categoria class categorieadmin(sqla.modelview): column_display_pk = true can_export = true export_types = ['xls'] list_columns = ['categoria', 'tipo'] column_formatters_export = dict( categoria=lambda v, c, m, p: '' if m.tipo none else str(m.tipo) )
Comments
Post a Comment