4

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

4

1 回答 1

5

最后,我在下面的帖子中找到了我的解决方案

我的错误是:

  • 在我的课上放一个db.relationship(CandidatLanguage) 。Candidat取而代之的是,我将关系放在我的“数据透视”表(CandidatLanguage)中。
  • 正如 IfLoop 在帖子中所说,我必须“从简单的多对多关系切换到“关联对象”。我不明白为什么,但它可以使用关联对象。

然后我的 models.py 现在看起来像这样:

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.Date)
    categories = db.relationship('Category', secondary=category_candidat,
                                 backref=db.backref('candidat', lazy='dynamic'))

    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 '%s' % unicode(self.name)


class Languagelevel(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128))

    def __repr__(self):
        return '%s' % 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'))
    language_level_id = db.Column(db.Integer, db.ForeignKey('languagelevel.id'))

    language = db.relationship(Language, backref="Candidat")
    candidat = db.relationship(Candidat, backref="Langue")
    languagelevel = db.relationship(Languagelevel, backref="Candidat")

我的views.py是这样的:

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

class CandidatView(ModelView):
    column_auto_select_related = True
    inline_models = (CandidatLanguage,)

admin.add_view(CandidatView(Candidat, db.session))
admin.add_view(ModelView(Language, db.session))
admin.add_view(ModelView(Languagelevel, db.session))

有了这个,一切正常,我有一个我想要的内联表单。

我还没有理解一切,但它的工作原理。如果有人可以解释,这是当然的欢迎。

问候

于 2014-02-24T12:38:46.453 回答