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)
我有两个模型,第一个有一个 int 字段,第二个是一个 int 并且我想要在用户在第二个字段中输入一个值后,它从第一个中减去,第一个更改的结果为其旧结果减去用户在第二个输入的值
您可以让来自模型的数据直接在 views.py 文件中相互交互。
例如,你可以有:
from .models import prodect, orders
def example(request):
prodect = prodect.objects.all()
orders = orders.objects.all()
foo = prodect.cwan - orders.count
return foo
您还可以向正在内部进行此数学运算的模型之一添加属性。
示例模型.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)
@property
def math(self):
if self.count:
x = self.count - self.prodects.cwan
return x
else:
return '-'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句