Flask-admin: 1.0.9 broke uselist=False on backref

Created on 18 Dec 2014  Â·  9Comments  Â·  Source: flask-admin/flask-admin

In 1.0.8, I could have relationships with uselist=False with no problem (it'd just look strange with how you could actually add multiple entries in the UI…), but in 1.0.9, this instead displays the same symptoms described in #376. And just like in #376, the fix is to move the uselist=False from the backref to the relationship.

Most helpful comment

Goal is to edit two related models at once? Also, is there a chance that another model might not exist (your foreign keys are nullable)?

In any case, I'd use wtforms machinery to do it: http://wtforms.readthedocs.org/en/latest/fields.html#wtforms.fields.FormField

Only problem with this approach - you will have to override populate_obj in your form and create instance of related model if related model does not exists.

All 9 comments

Flask-Admin never supported uselist=False on a backref and there was another bug #376 which was fixed.

I'll see if it is easy to make flask-admin work with o2o backrefs too.

Just curious, why was this issue was closed? We're having the same issue. Thanks.

You have to specify uselist=False on a relationship side and it'll work.

Ok, we tried adding it to the relationship and it comes up with the same error, that the inline one-to-one model is not iterable.

in models.py
class User(Base):
    __tablename__ = 'users'

    id = sa.Column(sa.Integer(), primary_key=True)
    profile = relationship('Profile', backref="user", uselist=False)

class Profile(Base):
    __tablename__ = 'profiles'

    id = sa.Column(sa.Integer(), primary_key=True)
    user_id = sa.Column(sa.Integer, sa.ForeignKey("users.id"), nullable=False)


in admin page:
class UserModelView(EditOnlyModelView):
    inline_models = (Profile, )

def initialize_admin(app):
    admin = Admin(app, name='bliss', template_mode='bootstrap3')
    admin.add_view(UserModelView(User, db))

Traceback:
  File '/lib/python2.7/site-packages/flask_admin/model/fields.py", line 44, in process
    res = super(InlineFieldList, self).process(formdata, data)
  File "/lib/python2.7/site-packages/wtforms/fields/core.py", line 888, in process
    for obj_data in data:
TypeError: 'Profile' object is not iterable

That's different one - inline models can't work with one-to-one relationships, they always assume it is one-to-many relationship.

Ok, thanks. Is there another way you would suggest for one-to-one relationships? It's possible to just override the call before process(formdata, data), and check if it's a list, otherwise add it to one, but this seems a bit blunt.

Goal is to edit two related models at once? Also, is there a chance that another model might not exist (your foreign keys are nullable)?

In any case, I'd use wtforms machinery to do it: http://wtforms.readthedocs.org/en/latest/fields.html#wtforms.fields.FormField

Only problem with this approach - you will have to override populate_obj in your form and create instance of related model if related model does not exists.

Great thanks! Yes, that's the idea (good point, that it could be nullable). I'll take a look at using wtforms here.

@nshelly, Any update on how/if you got this to work? I'm looking at using 1-to-1 inline model as well, possibly.

Was this page helpful?
0 / 5 - 0 ratings