Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask-admin: How to display meaningful foreign key info in edit and create forms?

I have a Flask-Admin interface to a flask app using SQLAlechemy, and I can't seem to figure out how to handle foreign keys. Specifically, I have the following two models:

class DoctorType(db.Model):
    __tablename__ = 'doctor_type'
    id = db.Column(db.Integer, primary_key=True)
    doctor_type_english_name = db.Column(db.Unicode(255))
    doctors = db.relationship('Doctor')

    def __unicode__(self):
        return self.doctor_type_english_name

class Doctor(db.Model):
    __tablename__ = 'doctor'
    id = db.Column(db.Integer, primary_key=True)
    doctor_english_name = db.Column(db.Unicode(255))
    doctor_type_id = db.Column(db.Integer, db.ForeignKey('doctor_type.id'))

    def __unicode__(self):
        return self.doctor_english_name

I'd like for the doctor_type_id field in the create or edit forms of flask-admin to be a dropdown list of doctor_type_english_name rather than just being an integer input like this:

Doctor Type Input Field

I have __unicode__ functions on the models (I'm using python 2.7). I tried tinkering with form_ajax_refs in my custom form views but with no success. Any help would be much appreciated!

like image 998
Matthew Markwell Avatar asked May 29 '17 22:05

Matthew Markwell


1 Answers

Implement __repr__ in define of data model.

class DoctorType(db.Model):
    #...
    def __repr__(self):
        return self. doctor_type_english_name
like image 80
stamaimer Avatar answered Nov 15 '22 01:11

stamaimer