Django: Add and Subtract from inventory in models

前端 未结 1 689
花落未央
花落未央 2021-01-28 20:40

I\'m trying to do something that I thought would be simple, but it has proven a bit challenging for me right now.

I am trying to build a simple ATM system for Banknotes

1条回答
  •  礼貌的吻别
    2021-01-28 21:08

    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.

    0 讨论(0)
提交回复
热议问题