在Django中将2个变量一起相乘

时间:2018-11-29 22:57:52

标签: python django

嘿,所以我想将2个Django查询集的值一起添加(或添加,实际上是任何聚合函数)。我已经尝试了多种方法,但仍然遇到许多错误,例如

Request Method:  GET
Request URL:     http://127.0.0.1:8000/paymentamount/
Django Version:  2.1.2
Exception Type:  TypeError
Exception Value: unsupported operand type(s) for *: 'QuerySet' and 'QuerySet'

型号:

class List(models.Model):
    vin = models.CharField(max_length=100, null=True, default=None)
    unit = models.CharField(max_length=10, null=True, default=None)
    year = models.CharField(max_length=4, null=True, default=None)
    make = models.CharField(max_length=100, null=True, default=None)
    model = models.CharField(max_length=100, null=True, default=None)
    trim = models.CharField(max_length=50, null=True, default=None)
    transmission = models.CharField(max_length=10, null=True, default=None)
    drivetrain = models.CharField(max_length=10, null=True, default=None)
    seats = models.IntegerField(default=0, null=False)
    kilometers = models.IntegerField(default=0, null=False)
    ....
   tier1book = models.FloatField(null=True, default=0)
   tierid = models.ManyToManyField('TDTier')

class TDTier(models.Model):
    listid = models.ManyToManyField('List')
    bank = models.CharField(max_length=100, null=True, default=None)
    year = models.CharField(max_length=5, null=True, default=None)
    tier = models.CharField(max_length=100, null=True, default=None)
    frontendltv = models.FloatField(null=True, default=0)
    backendltv = models.FloatField(null=True, default=0)
    interest = models.FloatField(null=True, default=0)

观看次数:

def payment(request):
    item = List.objects.all()
    item3 = List.objects.values_list('tier1book', flat=True)
    item6 = TD.objects.filter(
        list__year=F('year'), 
        kmbottom__lte=F('list__kilometers'), 
        kmtop__gte=F('list__kilometers')
    )
    item7 = TDTier.objects.filter(
        list__year=F('year'),
        tier=('5 Key')
    ).values_list('frontendltv', flat=True)

    item8 = item3 * item7

return render(request, 'paymentamount.html', {'item8':item8})

我知道尝试将两个列表加在一起是行不通的,但是,Pythonic解决方案似乎在Django中不起作用(例如for循环)。如果我执行原始SQL查询,但似乎也想避免这种情况,那么该解决方案似乎也是可行的。

谢谢!

1 个答案:

答案 0 :(得分:0)

这是怎么回事:item8 = [x * y是zip(item3,item7)中的x,y]? – schwobaseggl 11月30日在16:27

这是正确的答案!试图给予@schwobaseggl荣誉。

谢谢!