django patch 解决 ["'15428560000' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

__init__.py

import datetime

from django.apps import AppConfig
from django.db.models.fields import DateTimeField

default_app_config = 'patch.PatchConfig'


def patch():
    def get_db_prep_value(self, value, connection, prepared=False):
        if isinstance(value, str):
            try:
                value = datetime.datetime.fromtimestamp(int(value))
            except:
                raise

        if not prepared:
            value = self.get_prep_value(value)
        return connection.ops.adapt_datetimefield_value(value)

    DateTimeField.get_db_prep_value = get_db_prep_value


class PatchConfig(AppConfig):
    name = 'patch'

    def ready(self):
        patch()


目录结构
├── patch
│   └── __init__.py

settings.py

INSTALLED_APPS = [
    'patch',
    ....
   ]
posted @ 2018-11-22 13:41  两只老虎111  阅读(2326)  评论(0编辑  收藏  举报