Flask-Admin Many-to-Many field display
Asked Answered
B

2

4

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-many relationship between articles and tags tables it's clear. In the Flask-Admin interface when I try to add a tag to articel (or vise versa) it works fine. But it displays awfully and it impossible to choose correct tag because it displays like an object: enter image description here

And that is right because it is an instance of my model class. But how can I display here only a value of single field from that model? I really don't know how to do that. Is there any parameter for such kind of "form field" in the admin interface? I can find nothing. I hope that sombody know the solution.

Thanks for your help!

Bound answered 28/1, 2014 at 21:51 Comment(1)
Do you have a __unicode__ defined on the model ?Turnstone
A
6

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:

class MyModel(db.Model):
    """ Base class provides the __repr__ so that each model has short type. """
    __abstract__ = True

    def __unicode__(self):
        # attrs = db.class_mapper(self.__class__).column_attrs
        attrs = db.class_mapper(self.__class__).attrs # show also relationships
        if '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 in sorted(attrs)
                    )
                )
Alded answered 29/1, 2014 at 6:33 Comment(0)
L
8

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

def __str__(self):
    return self.name

worked just fine ;)

Loper answered 2/8, 2016 at 16:43 Comment(1)
works perfectly for python 3, which removed unicode()Gama
A
6

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:

class MyModel(db.Model):
    """ Base class provides the __repr__ so that each model has short type. """
    __abstract__ = True

    def __unicode__(self):
        # attrs = db.class_mapper(self.__class__).column_attrs
        attrs = db.class_mapper(self.__class__).attrs # show also relationships
        if '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 in sorted(attrs)
                    )
                )
Alded answered 29/1, 2014 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.