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.