Saturday, 11 December 2021

Flask-Admin inline_models empty attributes

I'm using Flask-Admin together with Flask-Sql-Alchemy.

My model contains of users that could have many vehicles. Issue is that when I open a user in Flask-Admin it shows shows the correct number of vehicles but all fields are empty.

This is related that I use collection_class=attribute_mapped_collection('id'), for the relationship. When I remove it the data is shown.

Any advice how to load the data without removing the collection_class attribute would be much appreciated.

Empty list of data sets:

enter image description here

Datamodel:

class Vehicle(db.Model):
    __tablename__ = 'vehicle'

    id = db.Column(db.Integer, primary_key=True, unique=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    license_plate = db.Column(db.String)
    user = db.relationship("User", back_populates='vehicles')



class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, unique=True)

    vehicles: {Vehicle} = db.relationship('Vehicle',
                                          collection_class=attribute_mapped_collection('id'),
                                          back_populates="user",
                                          cascade='all,delete-orphan')

Flask-Admin:


class UserModelView(ModelView):
    inline_models = (Vehicle)


def create_app():
    app = Flask(__name__)

    env = app.config['ENV']

    if env == "production":
        app.config.from_object('config.Config')
        app.secret_key = 'super secret key'
        app.config['SESSION_TYPE'] = 'filesystem'

    db.init_app(app)
    app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'

    admin = Admin(app, name='microblog', template_mode='bootstrap3')

    admin.add_view(UserModelView(User, db.session))
    
return app


from Flask-Admin inline_models empty attributes

No comments:

Post a Comment