Skip to content Skip to sidebar Skip to footer

Flask-admin Many-to-many Field Display

I develop an application using Flask. I use Postgres db (psycop2), SQLAlchemy and Flask-Admin for admin interface. And I got a problem and can't find a solution. I have many-to-man

Solution 1:

Adding this for people like me that stumble upon this answer, it didn't work for me, but

def__str__(self):
    returnself.name

worked just fine ;)

Solution 2:

You need to define __unicode__ method on your models. In an application where I am able to follow my own naming conventions, I might put the following in the base class for all my models and override where appropriate:

classMyModel(db.Model):
    """ Base class provides the __repr__ so that each model has short type. """
    __abstract__ = Truedef__unicode__(self):
        # attrs = db.class_mapper(self.__class__).column_attrs
        attrs = db.class_mapper(self.__class__).attrs # show also relationshipsif'name'in attrs:
            return self.name
        elif'code'in attrs:
            return self.code
        else:
            return"<%s(%s)>" % (self.__class__.__name__,
                ', '.join('%s=%r' % (k.key, getattr(self, k.key))
                    for k insorted(attrs)
                    )
                )

Post a Comment for "Flask-admin Many-to-many Field Display"