Django DateTime自动转换为IST字符串

时间:2016-04-26 05:52:42

标签: python django datetime django-models

在django中遇到这个奇怪的问题,当我将Obj作为函数传递时,我的Obj.date_time对象被转换为IST datetime-string。此外,仅在日志的基础上发生的情况很少。

#Actual Call
status_time_dict = {4: [orderObj.pickup_time,'dispatch_time'], 
                    5: [orderObj.delivered_time,'delivery_time']}

statusVar = status_time_dict.get(int(orderObj.status), [timezone.now(),"Undefined"])

statusDate = buildDateString(statusVar[0],orderObj, date_format_type = 0,convert_to_ist = 0 )
-----------------------------------------------------------------------

class Order(models.Model):
    name = models.CharField(max_length=100, blank=True)
    house_number = models.CharField(max_length=255)
    contact_number = models.CharField(max_length=13)
    order_date = models.DateField(db_index=True)
    order_time = models.DateTimeField(db_index=True) 
    pickup_time = models.DateTimeField(null=True)
    delivered_time = models.DateTimeField(null=True)

   def get_order_time(self):
       if self.order_time:
           return str(self.order_time.replace(tzinfo=None) + datetime.timedelta(minutes=330))
       else:
           return "not available"

    def get_pickup_time(self):
        if self.pickup_time:
            return str(self.pickup_time.replace(tzinfo=None) + datetime.timedelta(minutes=330))
        else:
            return "not available"

    def get_delivered_time(self):
        if self.delivered_time:
            return str(self.delivered_time.replace(tzinfo=None) + datetime.timedelta(minutes=330))
        else:
            return "not available"

def utc_to_local(utc_dt):
    local_tz = pytz.timezone('Asia/Kolkata')
    local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
    return local_tz.normalize(local_dt)


def buildDateString(statusDate,Obj, date_format_type = 1,convert_to_ist = 1):
    if statusDate is None:
        statusDate = timezone.now()

    elif type(statusDate) in [str, unicode]:
    # **This is where it is failing, it should not have ben str or      
    #   unicode as statusDate is being passed as OBJECT
    # OBJ.DATE_TIME should never been a string but is happeing for few 
    # cases only and that too in Prod server only.**
        failing_date = {"OID": Obj.id, "status_order": Obj.status, "failingDateString": statusDate,
            'pickup_time': Obj.pickup_time, 'allot_time': Obj.allot_time, 'delivered_time': Obj.delivered_time}

        print failing_date
        try:
            date_format = {True:"%Y-%m-%dT%H:%M:%S.%fZ", False:"%Y-%m-%d %H:%M:%S"}
            format = date_format.get('Z' in statusDate,"Y-%m-%d %H:%M:%S")
            statusDate = datetime.datetime.strptime(statusDate, format)
            if date_format_type == 1:
                return statusDate.strftime('%d-%m-%Y %H:%M:%S')
            else:
                return statusDate.strftime('%Y-%m-%d %H:%M:%S')
        except ValueError:
            statusDate = timezone.now()
    if convert_to_ist ==1:
        statusDate = utc_to_local(statusDate)
    if date_format_type == 1:
        statusDate = (statusDate).strftime('%d-%m-%Y %H:%M:%S')
    else:
        statusDate = (statusDate).strftime('%Y-%m-%d %H:%M:%S')
    return statusDate

请参阅 elif 中的评论,了解有关正在发生的错误的更多信息。

同样在我的models.py中,我定义了一个get_COLUMN_NAME函数,该函数转换为IST字符串,但在此上下文中没有调用它。我怀疑这是否是上述奇怪行为的原因

1 个答案:

答案 0 :(得分:0)

你必须检查orderObj的来源。可能一些其他代码在从数据库中获取对象后修改此对象的日期时间字段。