Django: Add and Subtract from inventory in models

淺唱寂寞╮ 提交于 2019-12-02 07:48:43

Everything looks fine in your model. You will have just a single row which will get be updated after it gets created first.

Firstly create the object of Cedula

from app.models import Cedula
cedula = Cedula()
cdeula.save() # This will instantiate the row with all entries 0.

For updating you have to perform this

from app.models import Cedula
from django.db.models import F
cedula = Cedula.objects.all()[0]
cedula.dios = F('dios')+ 2 # Here adding 2 or any number which you want to update with
cedula.vinte = F('vinte') -5 # Here subtracting with 5 or again number which you want to update with
cedula.save()

Usually for the models which don't have much relational things to do or single entries in them it is best to create their object in a seprate start script and run them before running your server. So that database can populate the content before.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!