__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',
....
]