I have a flask project, and I am getting started learning the flask-admin module.
SqlAlchemy schema for the required tables.
import datetime
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship
Base = declarative_base()
class Workgroup(Base):
__tablename__ = 'workgroups'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True,
autoincrement=True
)
name = sqlalchemy.Column(sqlalchemy.String(16))
shorthand = sqlalchemy.Column(sqlalchemy.String(4))
def __unicode__(self):
return self.name
class Drive(Base):
"""
A drive in an edit station.
"""
__tablename__ = 'drives'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True,
autoincrement=True
)
name = sqlalchemy.Column(sqlalchemy.String(64))
computer_id = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey(Computer.id)
)
computer = relationship('Computer', backref='drives')
is_active = sqlalchemy.Column(sqlalchemy.Boolean)
free_space = sqlalchemy.Column(sqlalchemy.BigInteger)
used_space = sqlalchemy.Column(sqlalchemy.BigInteger)
total_space = sqlalchemy.Column(sqlalchemy.BigInteger)
percentage_full = sqlalchemy.Column(sqlalchemy.Float)
boot_time = sqlalchemy.Column(sqlalchemy.DateTime)
last_changed_workgroup = sqlalchemy.Column(sqlalchemy.DateTime)
last_checked_in = sqlalchemy.Column(sqlalchemy.DateTime)
last_notified = sqlalchemy.Column(sqlalchemy.DateTime)
image_version = sqlalchemy.Column(sqlalchemy.String(64))
image_date = sqlalchemy.Column(sqlalchemy.DateTime)
current_workgroup_id = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey(Workgroup.id)
)
workgroup = relationship('Workgroup', backref='drives')
class DriveAdmin(sqla.ModelView):
column_display_pk = True
column_hide_backrefs = False
column_display_all_relations = True
form_columns = [ 'computer_id', 'workgroup.name', ]
column_list = ('computer.name', 'name', 'workgroup', 'computer.short_description', 'computer.notes',
'computer.station_type.description', 'computer.room.name')
class WorkgroupAdmin(sqla.ModelView):
column_display_pk = True # optional, but I like to see the IDs in the list
column_hide_backrefs = False
column_list = ('id', 'name', 'shorthand')
# Create admin
admin = admin.Admin(app, name='Example: SQLAlchemy2', template_mode='bootstrap3')
admin.add_view(WorkgroupAdmin(schema.Workgroup, db))
admin.add_view(DriveAdmin(schema.Drive, db))
replacing form columns for 'workgroup' with 'workgroup.name' gives me an invalid model property name, even though I have successfully used schema.workgroup.name elsewhere in code.
The resulting admin form looks like this.
How do I go about getting the workgroup.name value to appear as opposed to the object representation?
Thanks for reading!
You need to get the workgroup class to return its name via the repr function. That way it will show in the field.
class Workgroup(Base):
__tablename__ = 'workgroups'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True,
autoincrement=True
)
name = sqlalchemy.Column(sqlalchemy.String(16))
shorthand = sqlalchemy.Column(sqlalchemy.String(4))
def __unicode__(self):
return self.name
def __repr__(self):
return '<Workgroup %r>' % (self.name)