Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django models 模型实例法_Django Models - Fatal编程技术网

Django models 模型实例法

Django models 模型实例法,django-models,Django Models,以下是我的两个模型: class Bill(models.Model): client = models.ForeignKey(Client, on_delete=models.CASCADE) professional = models.ForeignKey(Professional, on_delete=models.CASCADE) date = models.DateField(default=timezone.now, blank=True) bill_status = models.

以下是我的两个模型:

class Bill(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
professional = models.ForeignKey(Professional, on_delete=models.CASCADE)
date = models.DateField(default=timezone.now, blank=True)
bill_status = models.BooleanField(initial=True)
close_date = models.DateField(blank=True, Null=True)
def __str__(self):
    return str(self.date) + " " +self.client.first_name +" " +self.client.last_name
class Meta:
    ordering = ['-date']

class BillableItem(models.Model):
bill = models.ForeignKey(Facture, on_delete=models.CASCADE)
date = models.DateTimeField(default=timezone.now, blank=True)
service = models.ForeignKey(Service, on_delete=models.CASCADE)
qty = models.FloatField()
price = models.SmallIntegerField()
我想要这样的东西

Bill.objects.filter(BillableItem__bill.aggregate(sub_total= Sum(F('qty') * F('price'), output_field=FloatField()))
      
但是作为一个模型实例方法,我不必每次都重写它。我对Django不太熟悉

下面是原始(我的)SQL的样子

select 
      sum(price*qty) 
from 
     Bill_Billable as a
left join 
     Bill_Bill as b on b.id = a.bill_id
where 
     b.id=1 <-That would change based on the instance
选择
金额(价格*数量)
从…起
比尔·努可作为一名律师付费
左连接
b.id=a.Bill\U id上的b账单
哪里
b、 id=1