0

I am using Flask-Admin to manage several tables in my database and I've stumbled upon one issue. I have one-to-many relations in my database and the model is ok. If I want to add/delete/display records from/into my database via the Admin interface everything is ok. The only problem is that the Foreign Key fields are shown as object and I have no idea how to switch this to names.

class Locations(db.Model):
    __tablename__ = "locations"
    id = db.Column('id', db.Integer, primary_key=True)
    name = db.Column('name', db.Unicode)
    code = db.Column('code', db.Unicode)
    servers = db.relationship('Servers', backref='location', lazy='dynamic')
    subnets = db.relationship('Subnets', backref='location', lazy='dynamic')

class Servers(db.Model):
    __tablename__ = "servers"
    id = db.Column('id', db.Integer, primary_key=True)
    serialnum = db.Column('serialnum', db.Unicode)
    hostname = db.Column('hostname', db.Unicode)
    type = db.Column('type', db.Unicode)
    locationid = db.Column('locationid', db.Integer, db.ForeignKey('locations.id'))
    idrac = db.relationship('Idracs', backref='server', lazy='dynamic')
    interface = db.relationship('Interfaces', backref='server', lazy='dynamic')

class ServersView(ModelView):
    column_searchable_list = ('hostname', 'type', 'serialnum')
    column_list = ('location', 'hostname', 'type', 'serialnum')
    form_columns = [ 'location', 'hostname', 'type', 'serialnum' ]

admin.add_view(ServersView(Servers, db.session))

When I want to list or create a new record I would like to see the name of the location, not the object main.Locations .

Can anyone give me a small hint ?

Thanks in advance!

  • So far, I've managed to get it right for the list/view by adding this to the `ModelView` : `column_list = ('location.name', 'hostname', 'type', 'serialnum')` But the form stays the same, I still cannot display the name property of the object. – Radu Stefanache Feb 10 '16 at 12:27
  • thanks, that was it! – Radu Stefanache Feb 10 '16 at 13:23
  • Hi. This solved the problem to the display list, but didn't solve the problem in the form select that comes populated with relationship backrefs. Any hint? – m3nda Aug 06 '22 at 07:17

0 Answers0