[英]Django: loop all the records in a table and then get field from another table
[英]Django Subtract a field from another field in another table and save the result
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 '-'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.