These are chat archives for pythonvietnam/Flask

11th
Apr 2017
Yarik Genza
@yarikgenza
Apr 11 2017 16:08

Hi, everyone. I'm new in flask and need help.

I have two models:

class Article(db.Model):
    id = db.Column('_id', db.Integer, primary_key = True)
    child = db.relationship('Child', lazy="dynamic")
    description = db.Column(db.String(1000))
    diagnosis = db.Column(db.String(100))
    amount = db.Column(db.Integer)
    img = db.Column(db.Integer, db.ForeignKey('img._id'))
    isActive = db.Column(db.Boolean, default = False)
    isVerified = db.Column(db.Boolean, default= False)

    def __init__(child, description, diagnosis, amount, img, isActive, isVerified):
        self.child = child
        self.description = description
        self.diagnosis = diagnosis
        self.amount = amount
        self.img = img
        self.isActive = isActive
        self.isVerified = isVerified

and

class Child(db.Model):
    id = db.Column('_id', db.Integer, primary_key = True)
    name = db.Column(db.String(100))
    surname = db.Column(db.String(100))
    article = db.Column(db.Integer, db.ForeignKey('article._id'))
    city = db.Column(db.String(100))
    birthday = db.Column(db.DateTime),
    mobile = db.Column(db.Integer)
    address = db.Column(db.String)
    skype = db.Column(db.String)
    email = db.Column(db.String)

    def __init__(name, surname, article, city, birthday, mobile, address, skype, email):
        self.name = name
        self.surname = surname
        self.article = article
        self.city = city
        self.birthday = birthday
        self.mobile = mobile
        self.address = address
        self.skype = skype
        self.email = email

So, I have Article and article.child field.

In my template i want to do something like this:

{% for article in Articles %}
       <div class="col s12 m4">
         <div class="icon-block center">
           <img src="{{ url_for('static', filename='main/img/two.jpg') }}" class="circle responsive-img">
           <h5 class="center">{{ article.diagnosis }}</h5>

           <p class="light">{{ article.child.name }}</p>
           <a class="waves-effect waves-light btn" href="#">More</a>
         </div>
       </div>
         {% endfor %}

But article.diagnosis - works.
But article.child.name - didn't( I have idea, why. In general, article.child is just a id, not object.
Canenyone help me to populate article with child model, to make it able to write like "atricle.child.name, article.child.surname" and so on.

Thanks.