Reputation: 153
To add the item with the given description I use: db.session.add(new_item)
. To delete the item: db.session.query(Item).filter_by(item_id=new_id).delete()
. To update some parts of the item: db.session.query(Item).filter_by(item_id=new_id).update({"status":"1 "})
.
What should I use if I want to edit the item completely, that is, reinsert the data for the same item?
here is the code for the form:
<form class="form" action="{{ url_for('new_item') }}" method="post" role="form" enctype=multipart/form-data>
{{ form.csrf_token }}
<table>
<tr>
<td>
<div class="form-group">
<label for="item_name">item name:</label>
<input name="name" type="text" class="form-control" id="item_name">
</div>
</td>
<td>
<div class="form-group">
<label for="item_price">item price</label>
<input name="price" type="number" class="form-control" id="item_price">
</div>
</td>
<td>
<div class="form-group">
<label for="photo">Download the photo</label>
<input type="file" name="file">
<p class="help-block">Download</p>
</div>
</td>
</tr>
<tr>
<td>
<div class="form-group">
<label for="item_category">Category:</label>
<select name="category" class="form-control" id="item_category">
<option>LEGO</option>
<option>Игры_и_игрушки</option>
<option>Малыш</option>
<option>Школа_и_канцтовары</option>
<option>Творчество_и_развитие</option>
</select>
</div>
</td>
</tr>
</table>
<div class="form-group">
<label for="item_description">Description of the item:</label>
<textarea name="description" class="form-control" id="item_description" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-default">Save</button>
</form>
Here is the route for the form. There are others as well, to update the items and to delete it, but i guess this should be enough
@app.route('/admin_items', methods=['GET', 'POST'])
def admin_items():
form = AddItemForm(request.form)
available = db.session.query(Item).filter_by(status='1').order_by(Item.name.asc())
not_available = db.session.query(Item).filter_by(status='0').order_by(Item.name.asc())
return render_template('admin_items.html',
available_items=available,
not_available_items=not_available,
form=form)
@app.route('/add_item', methods=['GET', 'POST'])
@login_required
def new_item():
error = None
form = AddItemForm(request.form)
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename) and form.name.data != "" and form.description.data != "":
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOADED_ITEMS_DEST'], filename))
new_item = Item(
filename,
form.name.data,
form.description.data,
form.price.data,
form.age.data,
form.particles.data,
form.category.data,
'1',
)
db.session.add(new_item)
db.session.commit()
return redirect(url_for('admin_items'))
else:
return render_template('admin_items.html', form=form, error=error)
if request.method == 'GET':
return redirect(url_for('admin_items'))
Upvotes: 1
Views: 128
Reputation: 9450
You can specify more elements to the update clause .update({"status":"1", "colour":"red"})
or you can grab the object from the database and just change it as required:
item = db.session.query(Item).get(1) # grab the item with PK #1.
item.status = '1'
item.colour = 'red'
db.session.commit() # commit your changes to the database.
Upvotes: 4