无法将datetime.timedelta()转换为Decimal django

时间:2015-04-25 04:25:24

标签: python django datetime

我从另一个日期减去一个日期,并从结果中减去8小时。之后我将结果转换为十进制。但在保存对象时我收到错误

Cannot convert datetime.timedelta(0, 21600) to Decimal

代码:

            fm.extra_hours = (fm.trip_closing_date - fm.trip_starting_date) - timedelta(hours=8)
            #fm.extra_hours = time - timedelta(hours=8)
            st = fm.extra_hours
            print type(st)
            print st.days
            seconds = st.total_seconds()
            hours = int(seconds // 3600)
            print hours
            print type(hours)
            ext_hrs = float (hours)

            print ext_hrs
            print type(ext_hrs)
            if st.days < 0:
                st = 0
            else:
                y="xyz"

            if Ek>=1:
                fm.extra_kilometers = Ek
                fm.extra_charge = ((((fm.vehical_type.per_extra_km_charge))*((fm.extra_kilometers)))+(((fm.vehical_type.per_extra_hour_charge))*(Decimal(ext_hrs))))
                fm.pure_amount = ((((fm.total_kilometer))*((fm.vehical_type.per_extra_km_charge)))+(((fm.vehical_type.per_extra_hour_charge))*(Decimal(ext_hrs))))
                fm.service_tax = (((fm.pure_amount)+(fm.driver_allownce_charge))*Decimal(4.944))/Decimal(100)
                fm.grand_total = ((fm.pure_amount)+(fm.driver_allownce_charge)+(fm.service_tax))
                fm.amount_payeble_now = (fm.grand_total)-(fm.less_adavance)
                fm.save()

我不知道我在做什么错?请帮助我。

完整追踪

    TypeError at /home/save_local_invoice/
Cannot convert datetime.timedelta(0, 21600) to Decimal
Request Method: POST
Request URL:    http://localhost:8000/home/save_local_invoice/
Django Version: 1.7
Exception Type: TypeError
Exception Value:    
Cannot convert datetime.timedelta(0, 21600) to Decimal
Exception Location: C:\Python27\lib\decimal.py in __new__, line 658
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.8
Python Path:    
['C:\\Users\\MAGOWA\\Desktop\\magowa',
 'C:\\Users\\MAGOWA\\Desktop\\magowa',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages']
Server time:    Sat, 25 Apr 2015 10:26:33 +0530

error on this line fm.save() 

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试将timedelta对象保存为Decimal。如果没有说明Decimal应该如何解释对象,这是不可能的。我想你会想做fm.my_decimal_field = my_time_delta_object.total_seconds()

之类的事情