在yyyymmddhhiiss

时间:2019-05-19 11:17:24

标签: python django datetime before-save

我在Django管理员中使用旧版数据库架构。我的日期时间字段是varchar(14),保存的格式为:YYYYmmddHHiiss。

我希望这些类型的“ datetime”字段具有与django模板中常见的datetime字段相似的行为。 (我的意思是,日期/时间小部件可以理解数据)。

我创建了这个自定义DateTime类:

# module u.py
# some helper functions
def get_objdatetime(strdatetime):
    # strdatetime = "20190102153348"
    strdatetime = strdatetime.replace(" ","").replace("-","").replace(":","")
    if len(strdatetime)<14:
        strdatetime += "0000" #patch
    objdatetimeutc = datetime.strptime(strdatetime,'%Y%m%d%H%M%S')#.strftime('%m/%d/%Y')
    return objdatetimeutc

def get_strdatetime(objdatetime):
    strdatetime = objdatetime.strftime('%Y%m%d%H%M%S')
    return strdatetime


class TheappDatetime(models.DateTimeField):

    def from_db_value(self, value, expression, connection, context):   
        return self.to_python(value)

    def to_python(self, value):
        if value is None:
        elif isinstance(value,str):
            value = u.get_objdatetime(value)
        else:
            value = u.get_strdatetime(value)
        return value

    # métodos al guardar
    def get_prep_value(self, value):
        if value is not None:
            value = u.get_strdatetime(value) #<-- bug here
        return value

    def value_to_string(self, obj):
        value = self.value_from_object(obj)
        return value

    def get_db_prep_value_(self, value, connection, prepared=False):
        if value==None:
            return None
        return u.get_objdatetime(value)

    def get_db_prep_save(self, value, connection, prepared=False):
        if value is not None:
            value = u.get_objdatetime(value)
        return self.get_db_prep_value(value, connection, prepared)

widgets

效果不好。因为即使它看起来不错,当我恢复数据时,小部件在尝试保存数据时也会理解它,但我得到了以下错误:

return value.utcoffset() is not None AttributeError: 'str' object has no attribute 'utcoffset'

此行引发此错误:value = u.get_strdatetime(value) #<-- bug here因为我试图在“ datetime”字段中存储字符串值。

如果我删除了 get_prep_value 方法的重写,则不会出现任何错误,但是保存值是错误的:

error-format-saved

我尝试覆盖许多粗糙的方法而没有成功。

这是文件堆栈,其中包含有关更改操作的方法:

File "<v-env>\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "<v-env>\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "<v-env>\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "<v-env>\lib\site-packages\django\contrib\admin\options.py", line 606, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "<v-env>\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "<v-env>\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "<v-env>\lib\site-packages\django\contrib\admin\sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "<v-env>\lib\site-packages\django\contrib\admin\options.py", line 1637, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
File "<v-env>\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "<v-env>\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "<v-env>\lib\site-packages\django\contrib\admin\options.py", line 1522, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "<v-env>\lib\site-packages\django\contrib\admin\options.py", line 1561, in _changeform_view
self.save_model(request, new_object, form, not add)
File "<project>\theapp\admin\sections\admin_theapp.py", line 50, in save_model
super().save_model(request, obj, form, change)
File "<v-env>\lib\site-packages\django\contrib\admin\options.py", line 1088, in save_model
obj.save()
File "<v-env>\lib\site-packages\django\db\models\base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "<v-env>\lib\site-packages\django\db\models\base.py", line 779, in save_base
force_update, using, update_fields,
File "<v-env>\lib\site-packages\django\db\models\base.py", line 851, in _save_table
forced_update)
File "<v-env>\lib\site-packages\django\db\models\base.py", line 900, in _do_update
return filtered._update(values) > 0
File "<v-env>\lib\site-packages\django\db\models\query.py", line 760, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "<v-env>\lib\site-packages\django\db\models\sql\compiler.py", line 1429, in execute_sql
cursor = super().execute_sql(result_type)
File "<v-env>\lib\site-packages\django\db\models\sql\compiler.py", line 1087, in execute_sql
sql, params = self.as_sql()
File "<v-env>\lib\site-packages\django\db\models\sql\compiler.py", line 1395, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "<project>\theapp\models\fields.py", line 74, in get_db_prep_save
return self.get_db_prep_value(value, connection, prepared)
File "<v-env>\lib\site-packages\django\db\models\fields\__init__.py", line 1430, in get_db_prep_value
return connection.ops.adapt_datetimefield_value(value)
File "<v-env>\lib\site-packages\django\db\backends\mysql\operations.py", line 194, in adapt_datetimefield_value
if timezone.is_aware(value):
File "<v-env>\lib\site-packages\django\utils\timezone.py", line 248, in is_aware
return value.utcoffset() is not None

我要保存20190518105449而不是2019-05-18 10:

0 个答案:

没有答案