Search code examples
pythonpeewee

Is there a functions for mathematical adding in peewee


I am still a beginner at Flask and peewee. I am trying to make a web app for a warehouse, it is supposed to run just locally. In the database, I have the model of the product, the manufacturer(proizvoditel), the price(cena) and the quantity(kolicina). My problem is when you need to update the quantity.

I tried using "+" but, I got an error.

models.py

class Magacin(Model):
    model=CharField(unique=True)
    proizvoditel=CharField()
    cena=IntegerField()
    kolicina=IntegerField()
    timestamp=DateTimeField(default=datetime.datetime.now)

app.py

@app.route("/", methods=["GET", "POST"])
def post():
    form=forms.VnesuvanjeForm()
    if form.validate_on_submit():
        models.Magacin.create(

                               model=form.model.data,
                               proizvoditel=form.proizvoditel.data,
                               cena=form.cena.data,
                               kolicina=form.kolicina.data)
        flash("Postiranoo", "success")
        return redirect(url_for("post"))
    return render_template("vnesuvanje.html", form=form)

For example, let's say the quantity(kolicina) is 50 and I need to update it for 20.After updating the quantity the quantity should be 70. I tried using the plus sign but it didn`t work. Should I make a new view using the update() method? Pardon me if I have mistakes asking the question.


Solution

  • In your code snippet you are creating a new row.

    If you simply wish to update a quantity, then you should use the .update() method:

    q = (Magacin
         .update({Magacin.kolicina: Magacin.kolicina + 20})
         .where(...))
    n = q.execute()