Flask-Admin: create child objects with parents in "create" form

In my Flask / SQLAlchemy application, I have the SQLAlchemy Parent and Child classes, where all the interesting data about each parent is in its children:

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship(Child)

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    name = Column(String)

      

I want to create Parents in Flask-Admin frontend and when I create them I want to be able to create my child objects at the same time. When I go to the "create" form in the ModelView for the default parent, I can only select existing children from the menu and not create new ones.

For example, when creating each parent, I would like to have text boxes where I can enter the "names" of the child objects, so that when Flask-Admin creates the parent, it also creates the children with those values ​​in their "name".

Is this possible with Flask-Admin? Or, if not, how do I configure the Flask-Admin ModelView to do this?

+3


source to share


1 answer


I think you are looking for inline models .

You can use them in your example with:



class ParentModelView(ModelView):
    inline_models = (Child, )

      

0


source







All Articles