Reputation: 91
I have a python app built using Flask, PeeWee and Sqlite. I have created a database table that is presented in a frontend view. I am trying to make it so that a user can click on an update/edit link and it will fire off a function that presents a form based on the ID of the table row.
The user can then use the form to update the table record.
The Problem: The form shows up as expected and I can add information and submit the form without errors however, nothing changes in the database i.e. the original record is still the same as before.
What I've tried: I've tried using the save() and update() functions that are in the PeeWee docs, but I'm either coding it incorrectly or missing things. So the front view with the form, sends the data to a function called 'change_standard' which should then update the database, but doesn't.
Relevant code is below. Thanks in advance :)
app.py
'''updates a standard'''
@app.route('/update_standard/<int:id>', methods=('GET', 'POST'))
@login_required
def update_standard(id):
form = forms.UpdateForm()
if form.validate_on_submit():
models.Standards.change_standard(
section=form.section.data,
standard=form.standard.data)
flash("Standard updated! Thanks!", "success")
return redirect(url_for('view_standards'))
return render_template('update-standard.html', form=form, id=id)
models.py
class Standards(Model):
section = CharField(unique=False)
standard = CharField(unique=True)
class Meta:
database = DATABASE
@classmethod
def create_standard(cls, section, standard):
try:
with DATABASE.transaction():
cls.create(
section=section,
standard=standard
)
except IntegrityError:
raise ValueError("Section or Standard already exists")
@classmethod
def delete_standard(cls, id, section, standard):
try:
with DATABASE.transaction():
cls.delete(
id=id,
section=section,
standard=standard)
except IntegrityError:
raise ValueError("Standard does not exist for deletion")
@classmethod
def change_standard(cls, section, standard):
try:
with DATABASE.transaction():
cls.update(
section=section,
standard=standard)
except IntegrityError:
raise ValueError("Standard does not exist for update")
forms.py
class UpdateForm(FlaskForm):
section = TextAreaField('Enter new section', validators=[DataRequired(), section_exists])
standard = TextAreaField('Enter new standard', validators=[DataRequired(), standard_exists])
Upvotes: 0
Views: 293
Reputation: 91
So here is what I went with thanks to a combination of @Tomasz Paluch's answer and a comment from another site. The bit I was missing was referencing the record ID in the right way.
@classmethod
def change_standard(cls, id, section, standard):
try:
with DATABASE.transaction():
standards_to_update = cls.get_by_id(id) # specifies which Standards to update
standards_to_update.section = section # updates the value for section
standards_to_update.standard = standard # updates the value for standard
standards_to_update.save() # saves changes
except IntegrityError:
raise ValueError("Standard does not exist for update")
Upvotes: -1
Reputation: 357
Your change_standard
method should look like this to take effect (specify the id of the updated row and execute the query):
@classmethod
def change_standard(cls, id, section, standard):
try:
with db.transaction():
cls.update(
section=section,
standard=standard).where(cls.id==id).execute()
except IntegrityError:
raise ValueError("Standard does not exist for update")
Reference link: Updating existing records
Upvotes: 2