Commit 911be801 authored by Serge S. Koval's avatar Serge S. Koval

Fixed #530. Do not fail with self-referential inline models

parent 0f4d0e4f
...@@ -550,7 +550,7 @@ class InlineModelConverter(InlineModelConverterBase): ...@@ -550,7 +550,7 @@ class InlineModelConverter(InlineModelConverterBase):
reverse_prop = None reverse_prop = None
for prop in target_mapper.iterate_properties: for prop in target_mapper.iterate_properties:
if hasattr(prop, 'direction'): if hasattr(prop, 'direction') and prop.direction.name in ('MANYTOONE', 'MANYTOMANY'):
if issubclass(model, prop.mapper.class_): if issubclass(model, prop.mapper.class_):
reverse_prop = prop reverse_prop = prop
break break
...@@ -560,8 +560,13 @@ class InlineModelConverter(InlineModelConverterBase): ...@@ -560,8 +560,13 @@ class InlineModelConverter(InlineModelConverterBase):
# Find forward property # Find forward property
forward_prop = None forward_prop = None
if prop.direction.name == 'MANYTOONE':
candidate = 'ONETOMANY'
else:
candidate = 'MANYTOONE'
for prop in mapper.iterate_properties: for prop in mapper.iterate_properties:
if hasattr(prop, 'direction'): if hasattr(prop, 'direction') and prop.direction.name == candidate:
if prop.mapper.class_ == target_mapper.class_: if prop.mapper.class_ == target_mapper.class_:
forward_prop = prop forward_prop = prop
break break
......
...@@ -149,3 +149,23 @@ def test_inline_form_ajax_fk(): ...@@ -149,3 +149,23 @@ def test_inline_form_ajax_fk():
eq_(loader.model, Tag) eq_(loader.model, Tag)
ok_('userinfo-tag' in view._form_ajax_refs) ok_('userinfo-tag' in view._form_ajax_refs)
def test_inline_form_self():
app, db, admin = setup()
class Tree(db.Model):
id = db.Column(db.Integer, primary_key=True)
parent_id = db.Column(db.Integer, db.ForeignKey('tree.id'))
parent = db.relationship('Tree', remote_side=[id], backref='children')
db.create_all()
class TreeView(ModelView):
inline_models = (Tree,)
view = TreeView(Tree, db.session)
parent = Tree()
child = Tree(parent=parent)
form = view.edit_form(child)
eq_(form.parent.data, parent)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment