Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Issue deleting from a sql database using Flask
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(150))
    last_name = db.Column(db.String(150))
    username = db.Column(db.String(150), unique=True)
    password = db.Column(db.String(150))
    sex = db.Column(db.String(20))
    birthdate = db.Column(db.String(50))
    joined = db.Column(db.DateTime(timezone=True), default=func.now())
    message = db.relationship('Message')



class Message(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    sender = db.Column(db.Integer, db.ForeignKey('user.username'))
    receiver = db.Column(db.String(100))
    message = db.Column(db.String(10000))
    time_sent = db.Column(db.DateTime(timezone=True), default=func.now())



@views.route('/delete_message', methods=['POST'])
def delete_message():
    message = json.loads(request.message)
    message_id = message['id']
    message = Message.query.get(message_id)
    if message:
        if message.sender == current_user.username:
            db.session.delete(message)
            db.session.commit()

    return jsonify({})


function delete_message(message_id) {
  fetch("/delete_message", {
    method: "POST",
    body: JSON.stringify({ message_id: message_id }),
  }).then((_res) => {
    window.location.href = "/";
  });
}



        <button type="button" class="close" onClick="delete_message({{ message.id }})">
        <span aria-hidden="true">&times;</span>
        </button>

The first two classes are part of the python file to set up the database. The decorated function is part of the views python file. The next function is part of the javascript file. And the bottom section is part of the html file.

I am creating a message system, and I am having trouble deleting a message from an sql database using javascript and Flask. Pycharm says that "message" is an unresolved attribute reference to the class "request". Can anyone help me understand why the message will not delete form the database.db file.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use request.json() mehtod to get your json data

@views.route('/delete_message', methods=['POST'])
def delete_message():
    message = request.json()
    message_id = message['id']
    message = Message.query.get(

Br

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!