[Python] My model in database using SQLAlchemy and Flask cannot save data. TypeError:...

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I am getting the response from a LLM called Moondream from Ollama. And there is nothing wrong with it because I tried to debug by print out its response, and it was fine. I believe that the problem lies in the database. I have two models in my database, one is "users" and the other is "personal_guide". The "users" works fine with authentication (log in, sign up) but the personal_guide just cannot. It always gets unexpected keyword argument.

    Here is my models.py:

    class users(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(100), unique=True, nullable=False)
    name = db.Column(db.String(100), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    personal_guide = db.relationship('personal_guide')
    def __init__(self, email, name, password):
    self.email = email
    self.name = name
    self.password = password

    class personal_guide(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    def __init__(self, content, user_id):
    self.content = content
    self.user_id = user_id



    Here is my views.py (I use the Blueprint - auth and views instead of app):

    @views.route('/', methods=["GET", "POST"])
    @login_required
    def homepage():
    if request.method == "POST":
    user_input = request.form.get('user_input')
    response = str(moondream(user_input))
    print(response) #for debugging purpose
    print(current_user.id) #for debugging purpose
    try:
    user_info = personal_guide(content=response, user_id=current_user.id)
    db.session.add(user_info)
    db.session.commit()
    ai = personal_guide.query.filter_by(content=response).first()
    print(f"Retrieved guide content: {ai.content}, for user_id: {ai.user_id}") #for debugging purpose
    flash("Content saved", category='success')
    except Exception as e:
    print(f"Error: {e}")
    flash("Error saving content", category='failure')

    return render_template('homepage.html', response=ai.content)
    return render_template('homepage.html', current_user=current_user)


    I check word for word, and try to print out response and user_id, all seem to work correctly. I cannot understand what is the problem.

    And the result is that it never save the response to the model's content. Please help me! I would really appreciate that! Thanks a lot.

    Continue reading...

Compartilhe esta Página