简体   繁体   中英

How to get data from flask-sqlalchemy database

I have a problem with getting data from my database using flask-sqlalchemy. I want take "answers" for each person and show them in my admin panel. I created a database with relationship (one to many) and everything is working pretty well however I am unable to print them on my website. Less talking more coding.

Here is my code for my database

class User(db.Model,UserMixin):
  id = db.Column(db.Integer, primary_key=True)
  username = db.Column(db.String(20), unique=True, nullable=False)    
  password = db.Column(db.String(60), nullable=False)
  answers = db.relationship('Answer', backref='author', lazy=True)

def __repr__(self):
    return f"User('{self.username}', '{self.password}')"


class Answer(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
  day = db.Column(db.Text, nullable=False)
  user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

def __repr__(self):
    return f"Post( '{self.date_posted}')"

Here is my code for my route (there is nothing but trust me I tried everything)

@app.route('/users')
def users():
   users = User.query.all()
   fusernames = User.query.order_by(User.username).all()
   answers = Answer.query.filter_by(day = fusernames)
   return render_template('users.html',users=users,answers=answers)

And here is my html template

{% extends "admin.html" %}
{% block content %}
{% for user in users %}

<div class="contentUsers">
<div class="Userbox">
<div class="login">Login: {{user.username}}</div>
<div class="passwordUser"> Hasło: {{user.password}}</div> 
<div class="AnswersDays">Day1: </div>   
<div class="answers">{{ user.answers }}</div>
</div> 
</div>
{% endfor %}
{% endblock content %}

So I want to show all the answers that my users provide. I check it and everything works however I have no idea how to show exact answer for exact user. Here you have image of the website (don't judge lol) 网站

I have to admit that I am new to this so it can be very simple but I really checked everything, read whole documentation watched dozen of tutorials and nothing. Some pieces of the code can be strange but I was trying my best.

Since your have created the one to many relationship between the User model and the Answer model. When you call relationship attribute user.answers , you will get a list of Answer objects as you see in your template. To show all the info of these answers, you have to add a for loop. Just replace this line

<div class="answers">{{ user.answers }}</div>

with:

<div class="answers">
    <ul>
    {% for answer in user.answers %}
        <li>{{ answer.day}} - {{ answer.date_posted }}</li>
    {% endfor %}
    </ul>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM