flask-admin not showing foreignkey columns

screenshot345 picture screenshot345 · Apr 23, 2013 · Viewed 8.9k times · Source
class Parent(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(120))

    def __repr_(self):
        return '<Parent %r>' % (self.name)

admin.add_view(ModelView(Parent, db.session))


class Child(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(120))
    parent = db.Column(db.Integer, db.ForeignKey(Parent))

admin.add_view(ModelView(Child, db.session))

Hello -

The code above is an example of flask-admin page that I am trying to create. The goal is to have on Child's create page a text box for name and a drop down to select a parent.

With the above setup, there is only name field. The parent drop down box is missing.

Any ideas how to make it happen?

Answer

Jasmijn picture Jasmijn · Apr 24, 2013

How about if you change the Child class to this:

class Child(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(120))
    parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))
    parent = db.relationship('Parent', backref=db.backref('children', lazy='dynamic'))

I don't know much about this, and I don't know if you need the back reference, but this setup works for me with Flask-Admin.