简体   繁体   English

如何从 flask-sqlalchemy 数据库中获取数据

[英]How to get data from flask-sqlalchemy database

I have a problem with getting data from my database using flask-sqlalchemy.我在使用 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这是我的 html 模板

{% 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.由于您已经在User model 和Answer model 之间创建了一对多关系。 When you call relationship attribute user.answers , you will get a list of Answer objects as you see in your template.当您调用关系属性user.answers时,您将获得一个Answer对象列表,如您在模板中看到的那样。 To show all the info of these answers, you have to add a for loop.要显示这些答案的所有信息,您必须添加一个 for 循环。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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