I'm currently starting a project with Flask. I'm currently experiencing Flask-Admin.
I try to setup a 2 levels relationship. For each "Candidat" I like to associate a language and a level (LanguageLevel) for this language.
Here is my models.py
from app import db
class Candidat(db.Model):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(128))
lastname = db.Column(db.String(128))
birthdate = db.Column(db.DateTime)
languages = db.relationship("CandidatLanguage", backref="candidat")
def __repr__(self):
return '<Nom %r>' % self.lastname
class Language(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128))
def __repr__(self):
return '<Langues %r>' % self.name
class Languagelevel(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128))
def __repr__(self):
return '<Niveau : %r>' % self.name
class CandidatLanguage(db.Model):
id = db.Column(db.Integer, primary_key=True)
candidat_id = db.Column(db.Integer, db.ForeignKey('candidat.id'))
language_id = db.Column(db.Integer, db.ForeignKey('language.id'))
langguage_level_id = db.Column(db.Integer, db.ForeignKey('languagelevel.id'))
Here is my views.py
from models import Candidat, Languagelevel, Language, CandidatLanguage
from flask.ext.admin import Admin, BaseView, expose
class MyView(BaseView):
@expose('/')
def index(self):
return self.render('adm-index.html')
admin = Admin(app)
admin.add_view(MyView(name='Hello'))
from flask.ext.admin.contrib.sqla import ModelView
admin.add_view(ModelView(Candidat, db.session))
admin.add_view(ModelView(CandidatLanguage, db.session))
admin.add_view(ModelView(Language, db.session))
admin.add_view(ModelView(Languagelevel, db.session))
What I would like to have is a Candidat form wich looks like this:
Firstname : ____
Lastname : _____
Language :
DropDown for Language :
- Language 1
- Language 2
DropDown for LanguageLevel :
- Level 1
- Level 2
For one "Candidat", one Language can only be associated with one LanguageLevel.
I've tried column_auto_select_related
, column_display_all_relations
, and of course none of them works like I thought. Of course this is my mistake.
If someone can show me the right way for reaching my goal it would be really helpfull.
Thanks in advance.
Regards