Python Django从另一个表中的另一个字段中减去一个字段并保存结果

Python Django从另一个表中的另一个字段中减去一个字段并保存结果,python,django,django-models,Python,Django,Django Models,我有两个模型,第一个是int字段,第二个是int字段 我希望在用户在第二个字段中输入一个值后,从第一个字段中减去第一个字段,第一个字段的结果更改为其旧结果减去用户在第二个字段中输入的值。您可以在views.py文件中直接让模型中的数据相互交互 例如,您可以: class prodect(models.Model): name = models.CharField(max_length=50) cwan = models.DecimalField(max_digits=5,

我有两个模型,第一个是int字段,第二个是int字段
我希望在用户在第二个字段中输入一个值后,从第一个字段中减去第一个字段,第一个字段的结果更改为其旧结果减去用户在第二个字段中输入的值。您可以在views.py文件中直接让模型中的数据相互交互

例如,您可以:

class prodect(models.Model):
      name = models.CharField(max_length=50)
      cwan = models.DecimalField(max_digits=5, decimal_places=2)
class orders(models.Model):
      names = models.CharField(max_length=50)
      prodects = models.ForeignKey(prodect,on_delete=models.CASCADE)
      count = models.DecimalField(max_digits=7, decimal_places=2)
您还可以将一个属性添加到一个在内部进行此计算的模型中

示例models.py

from .models import prodect, orders
def example(request):
    prodect = prodect.objects.all()
    orders = orders.objects.all()
    foo = prodect.cwan - orders.count
    return foo

您可以在views.py文件中让模型中的数据直接相互交互

例如,您可以:

class prodect(models.Model):
      name = models.CharField(max_length=50)
      cwan = models.DecimalField(max_digits=5, decimal_places=2)
class orders(models.Model):
      names = models.CharField(max_length=50)
      prodects = models.ForeignKey(prodect,on_delete=models.CASCADE)
      count = models.DecimalField(max_digits=7, decimal_places=2)
您还可以将一个属性添加到一个在内部进行此计算的模型中

示例models.py

from .models import prodect, orders
def example(request):
    prodect = prodect.objects.all()
    orders = orders.objects.all()
    foo = prodect.cwan - orders.count
    return foo

请分享你迄今为止所做的事情。你能上传你的观点吗?请分享你迄今为止所做的事情。你能上传你的观点吗?