I begin with Flask Admin, I have a problem that I cannot solve, even after several hours looking for a way to solve it in the documentation.
I changed the name of the entities for more understanding, but it's exactly the same problem.
I have a Many-To-Many relationship (my problem would be the same with a One-To-Many relationship) between an entity User and an entity Skill : a User can have several skills, and different users can have the same skills.
On the page of creation (or edition) of the entity Skill, there is a field for the "Users" relationship.
I have more than 100 000 Users, and when I click on the field it's really slow (due to the Javascript search script).
To make it faster, I only want to search into the 'active' Users (active is a boolean field in my SQL Database), because there are really a few active Users, and I will never add skill to an inactive User.
How can I do that ?
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin.contrib.sqla import ModelView
from flask.ext.admin import Admin
app = Flask(__name__)
db = SQLAlchemy(app)
admin = Admin(app)
association_table = db.Table(
'user_skill',
db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
db.Column('skill_id', db.Integer, db.ForeignKey('skill.id'), primary_key=True)
)
class Skill(db.Model):
name = db.Column(db.String(30), nullable=True)
class User(db.Model):
username = db.Column(db.String(30), nullable=True)
skills = db.relationship(
'Skill',
secondary=association_table,
backref='users'
)
class SkillView(ModelView):
# Something like that
form_relationship_query = {'users': lambda query: query.filter_by(active=True).all()}
admin.add_view(SkillView(Skill, db.session))