user3052646
user3052646

Reputation: 71

How do I update table in Flask-SQLAlchemy?

To add the task, I did:

task = TaskList(task_name=form_add_task.task_name.data, doer=current_user) db.session.add(task) db.session.commit()

Now, user and tasks has one to many relationship. What I am trying to do in the UI is display checkbox for each of the task for the specific user(task_status column). If the user selects checkboxes(which could be one or many) and click on update button, I want to change the task_status to 1 by updating it.

How do I just update the task_status to 1 for a specific user if they select one or more checkboxes?

models.py

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, nullable=False, index=True)
    email = db.Column(db.String(120), unique=True, nullable=False, index=True)
    password_hash = db.Column(db.String(128))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)
    tasks = db.relationship('TaskList', backref='doer', lazy='dynamic')

class TaskList(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    task_name = db.Column(db.String(140), nullable=False)
    task_status = db.Column(db.Integer, default=0)
    date_created = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

forms.py

class AddTaskForm(FlaskForm):
    task_name = StringField('task', validators=[DataRequired()])
    add_task_submit = SubmitField('Add Task')

class UpdateTaskForm(FlaskForm):
    task_status = BooleanField()
    update_task_submit = SubmitField('Update Task')

routes.py

@app.route('/mytask', methods=['POST', 'GET'])
@login_required
def my_task():
    form_add_task = AddTaskForm()
    form_update_task = UpdateTaskForm()
    if form_add_task.validate_on_submit():
        task = TaskList(task_name=form_add_task.task_name.data, doer=current_user)
        db.session.add(task)
        db.session.commit()
    user = User.query.filter_by(username=current_user.username).first()
    task_list = user.tasks.filter_by(task_status=0)

    if form_update_task.validate_on_submit():
        if form_update_task.task_status.data is True:
        **HOW DO I UPDATE THE CHECKBOXES**

    return render_template('my_task.html', title='my tasks',
                           form_add_task=form_add_task,
                           form_update_task=form_update_task,
                           task_list=task_list,
                           user=user
                           )

Upvotes: 1

Views: 2304

Answers (1)

Simas Joneliunas
Simas Joneliunas

Reputation: 3118

Firstly, you need something in your update task form to identify your task. Right now the object only has a boolean field but it has no way to tell flask or sql alchemy on which task should be updated. I would thus add task.id or, following your logic, task.task_name fields to the update form.

class UpdateTaskForm(FlaskForm):
    #task_id = IntegerField(#add relevant params here#)
    task_name = StringField('task', validators=[DataRequired()])
    task_status = BooleanField()
    update_task_submit = SubmitField('Update Task')

Afterwards i would use the task.id or task.task_name fields to fetch the task that needs to be updated from the database, set the task_status field to 1 and persist it.

task = TaskList.query.filter_by(id=form_update_task.id).first()
# or
# task = TaskList.query.filter_by(task_name=form_update_task.name).first()
if task is not None:
    task.task_status = 1
    db.session.add(task)
    db.session.commit()

Upvotes: 1

Related Questions