0

编辑:事实证明我不相信迁移与导致此错误有任何关系。

在下面的代码部分中,我选择聚合值并将这些值分配回使用模型,我发现自从我将应用程序从 django 开发服务器迁移到 apache 后,这些值没有被分配。

我的调试输出显示,在分配了这些值之后,该值从未实际应用于模型实例,当稍后进行保存时,这也反映了模型实例中仍然存在零。

蟒蛇代码

    cursor.execute("""select CUSTNBR,
                             FROMDATE,
                             sum(DECTHERMS) as DECTHERMS,
                             max(PRICE) as PRICE,
                             sum(SALESDLRS) as SALESDLRS,
                             sum(MCFS) as MCFS
                    from LQPPNATGAS
                    where CUSTNBR = ? and 
                          FROMDATE <> '20000000' 
                    group by CUSTNBR, FROMDATE """,str(cust_number))


    row = cursor.fetchone()
    while row:
        if str(row.FROMDATE)[0:4] >= str(oldest_year): 

            b = Usage.objects.filter(user_id=user_id,from_date=row.FROMDATE)

            b[0].decatherms = row.DECTHERMS
            b[0].price = row.PRICE
            b[0].sales_dollars = row.SALESDLRS
            b[0].mcf = row.MCFS
            b[0].save(update_fields=['decatherms','price','sales_dollars','mcf'])

            print >> sys.stderr ,str(b[0].sales_dollars)+"="+str(row.SALESDLRS)


        row = cursor.fetchone()

在error.log中输出

[Thu Aug 29 07:47:15 2013] [error] 0.00=168.22
[Thu Aug 29 07:47:15 2013] [error] 0.00=16.78
[Thu Aug 29 07:47:15 2013] [error] 0.00=1107.88
[Thu Aug 29 07:47:15 2013] [error] 0.00=676.85
[Thu Aug 29 07:47:15 2013] [error] 0.00=28.38
[Thu Aug 29 07:47:15 2013] [error] 0.00=876.94
[Thu Aug 29 07:47:15 2013] [error] 0.00=833.21
[Thu Aug 29 07:47:15 2013] [error] 0.00=26.33
[Thu Aug 29 07:47:15 2013] [error] 0.00=130.85
[Thu Aug 29 07:47:15 2013] [error] 0.00=1058.53
4

1 回答 1

0

我通过将列表的 [0] 元素分配给另一个变量 b,然后对该变量执行操作来解决问题,而不是在执行操作时指定 [0]。

修改后的python代码

   cursor.execute("""select CUSTNBR,
                             FROMDATE,
                             sum(DECTHERMS) as DECTHERMS,
                             max(PRICE) as PRICE,
                             sum(SALESDLRS) as SALESDLRS,
                             sum(MCFS) as MCFS
                    from LQPPNATGAS
                    where CUSTNBR = ? and 
                          FROMDATE <> '20000000' 
                    group by CUSTNBR, FROMDATE """,str(cust_number))


    row = cursor.fetchone()
    while row:
        if str(row.FROMDATE)[0:4] >= str(oldest_year):

            b = Usage.objects.filter(user_id=user_id,from_date=row.FROMDATE)[0]

            print >> sys.stderr , "before b.decatherms="+str(b.decatherms)

            b.decatherms = row.DECTHERMS
            b.price = row.PRICE
            b.sales_dollars = row.SALESDLRS
            b.mcf = row.MCFS
            b.save() #(update_fields=['decatherms','price','sales_dollars','mcf'])

            print >> sys.stderr , "after b.decatherms="+str(b.decatherms)

        row = cursor.fetchone()

在error.log中输出

[Fri Aug 30 10:37:28 2013] [error] before b.decatherms=0.0
[Fri Aug 30 10:37:28 2013] [error] after b.decatherms=44.9
[Fri Aug 30 10:37:28 2013] [error] before b.decatherms=0.0
[Fri Aug 30 10:37:28 2013] [error] after b.decatherms=91.1
[Fri Aug 30 10:37:28 2013] [error] before b.decatherms=0.0
[Fri Aug 30 10:37:28 2013] [error] after b.decatherms=3.8
[Fri Aug 30 10:37:28 2013] [error] before b.decatherms=0.0
[Fri Aug 30 10:37:28 2013] [error] after b.decatherms=2.9
[Fri Aug 30 10:37:28 2013] [error] before b.decatherms=0.0
[Fri Aug 30 10:37:28 2013] [error] after b.decatherms=11.8
于 2013-08-30T14:43:15.730 回答