flask-admin not showing foreign key columns in create form

98 Views Asked by At

I'm new to flask and developing and admin portal. I've pasting a simplified example here. We have a Seat model, and a Color model. There's a one to many relationship between Color and Seat.

from app import db

class Seat(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(20), index=True, unique=True)
    weight = db.Column(db.Integer())
    color_id = db.Column(db.Integer(), db.ForeignKey('color.id'))

    def __repr__(self):
        return '<Seat {}>'.format(self.name)


class Color(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(20), index=True, unique=True)

    seats = db.relationship('Seat', backref='seat_color', lazy='dynamic')

    def __repr__(self):
        return '<Color {}>'.format(self.name)

The admin models are defined as follows:

from flask_admin.contrib.sqla import ModelView
from flask_admin import Admin
from app import app, db
from app.models import Seat, Color


class SeatModelView(ModelView):
    column_list = ('id', 'name', 'weight', 'color_id')
    column_labels = {
         'id': 'ID',
         'name': 'Name',
         'weight': 'Weight',
         'color_id': 'Id of Color'
    }


class ColorModelView(ModelView):
    column_hide_backrefs = False
    column_list = ('id', 'name')
    column_labels = {
        'id': 'ID',
        'name': 'Color Name'
    }


admin = Admin(app, name='Test App Admin', template_mode='bootstrap3')
admin.add_view(SeatModelView(Seat, db.session))
admin.add_view(ColorModelView(Color, db.session))

But in my Admin portal, when I try to create a Seat, I can't see the option to select Color. Screenshot - Foreign key model can't be selected

I can create objects of Color model, and they have a valid string representation. The same style of code is working in another project. Example here: Screenshot - Foreign key model can be selected

I have checked other related questions but none were helpful in my case. Any suggestions would be greatly appreciated :)

0

There are 0 best solutions below