0

media is displaying like list

How to display file_name instead of list instance? i got this relationship

class wards(db.Model):
    __tablename__ = "wards"
    id = db.Column(db.Integer,primary_key=True)
    first_name = db.Column(db.String(50))
    last_name = db.Column(db.String(50))
    about = db.Column(db.Text(300))
    aim = db.Column(db.Text(300))
    geo_point_x = db.Column(db.Float)
    geo_point_y = db.Column(db.Float)
    fixed_summa = db.Column(db.Boolean(1))
    summa = db.Column(db.Integer)
    type = db.Column(db.String(100))
    images = db.Column(db.String(300),nullable = True, default='default.jpg')
    media = db.relationship('media')

    pass

class media(db.Model):
    id = db.Column(db.Integer, primary_key= True)
    ward_id = db.Column(db.Integer,db.ForeignKey('wards.id'))
    file_name = db.Column(db.String())
    pass

this is edit_form function that calls while editing

    def edit_form(self, obj=None):
        form = super(ViewWards,self).edit_form(obj)
        for item in form.data["media"]:
            print(item.file_name)
        return form

I can get file_name in terminal, but how to get it in browser?

Im new on stackoverflow.

I've tried to override edit_form, but cant find solution. There is lack of examples on flask-admin, and documentation is very confusing.

David
  • 1
  • 2
  • @pjcunningham, im not sure what i need to do with __rerp__ and __str__, form_extra_fields accept only class so i cant load methods in it. I need to return file_name instead of thing. I read that edit_form is sending form wich include media list, so i tried to override it but no effect. Sorry im new in flask-admin so i not quite understand how it works. – David Jul 20 '23 at 00:00
  • In your `Media` class you need to define a `__str__` method which returns the human readable form of instances of the class `Media`. In Flask-Admin the underlying form widget for the relationship will use the `__str__` method for display purposes. In your situation `__str__` method should return `self.file_name`. – pjcunningham Jul 20 '23 at 08:22
  • @pjcunningham yeah, that's exactly what i was looking for, ty very much! – David Jul 20 '23 at 12:48

0 Answers0