Flask-admin one-to-many relationship issue?

34 Views Asked by At

I am trying to achieve something that seems simple and should not take much effort, but i cannot...

I have database that uses SQLite3 and i use flask-sqlalchemy with flask-admin. in my models i have 2 models and ModelView. This is in short what my code looks like and am getting Exception: Cannot find reverse relation for model <class 'project.models.Parent'>

class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    name = db.Column(db.String, nullable=False)
    # relationship with parent and child
    parent = db.relationship('Child', backref='parent')
    
    
class Child(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    name = db.Column(db.String, nullable=False)
    born_date = db.Column(db.DateTime, nullable=False)
    
    # Foreign key to link child with parent by id
    parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))


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


admin.add_view(ParentView(Parent, db.session))

What i want to achieve is that one parent can have many children and this is what i think i should get with inline_models What i want Img is taken from another question so ignore the red circles. What i want is kinda like in the picture one parent and many children (values) and being able to add more children with add btn or something.

I have not tried very different things from what iam posting becouse im just not sure how to solve this and searching the web ive found simmilar issues from 7 to 8 years ago.

Thanks in advance for the help!

0

There are 0 best solutions below