[Flask] Populating child models with parent model Fields in Views

Anonymous Coder anonymouscodar at gmail.com
Wed Mar 1 01:51:56 EST 2017


Hi,
I need to populate model fields from parent model fields with ForeignKey
relation. Following is the example:

class Parent(db.Model):
     __tablename__='parent'
     .......
     id = db.Column(db.Integer, primary_key=True, autoincrement=True)
     child = relationship('Child", backref=('parent'))
     ....
class Child(db.Model):
     __tablename__='child'
     .....
     id = db.Column(db.Integer, primary_key=True, autoincrement=True)
     parent_id = db.Column(db.Integer, ForeignKey('parent.id'))
     ....
Id does shows the list but when I create a child model from views it is not
getting relevant parent_id automatically loaded into child model in
parent_id.

For example in my view if I create child model based on any condition:

if something:
    child = Child(other_filed=created_values, ....)
    db.session.add()
    db.session.commit()
It shows all the fields other than the relevant parent model automatically
attached to it.

Is there a way to bind the session with parent and get attributes of parent
to automatically associated with child model creation?

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20170301/b3e5629f/attachment.html>


More information about the Flask mailing list