django 在迁移时使用“make_aware要求初始日期时间”

von4xj4u  于 2022-11-26  发布在  Go
关注(0)|答案(1)|浏览(184)

我已经开发了一个应用程序与Django。这是工作在我的电脑与sqlite后端罚款。
但当我试图去生活与linux服务器和mysql后端,然后我得到bellow错误,而第一次迁移。

(env-bulkmailer) [root@localhost bulkmailer]# python3 manage.py migrate
Traceback (most recent call last):
  File "/var/www/bulkmailer-folder/bulkmailer/manage.py", line 22, in <module>
    main()
  File "/var/www/bulkmailer-folder/bulkmailer/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/core/management/commands/migrate.py", line 114, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/migrations/loader.py", line 58, in __init__
    self.build_graph()
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/migrations/loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/migrations/recorder.py", line 82, in applied_migrations
    return {
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/models/query.py", line 394, in __iter__
    self._fetch_all()
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/models/query.py", line 1866, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/models/query.py", line 117, in __iter__
    for row in compiler.results_iter(results):
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/models/sql/compiler.py", line 1336, in apply_converters
    value = converter(value, expression, connection)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/db/backends/mysql/operations.py", line 331, in convert_datetimefield_value
    value = timezone.make_aware(value, self.connection.timezone)
  File "/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/utils/timezone.py", line 291, in make_aware
    raise ValueError("make_aware expects a naive datetime, got %s" % value)
ValueError: make_aware expects a naive datetime, got 2022-11-20 12:39:18.866299+00:00

在设置中-

USE_TZ = True

我已经运行mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql也作为django文档。
我正在使用Django 4.1.3和MySQL社区8.0.30
先谢谢你。

eivnm1vs

eivnm1vs1#

遇到了同样的问题。在某个时候,django假设数据是时区初始的,没有检查。下面是解决方法。
在此处更新堆栈跟踪中列出的make_aware函数:

/var/www/bulkmailer-folder/env-bulkmailer/lib64/python3.9/site-packages/django/utils/timezone.py", line 291, in make_aware

如果该值是已知的,则不必引发错误,只需返回已知的值即可。请参阅下面最后一条else语句。

def make_aware(value, timezone=None, is_dst=NOT_PASSED):
    """Make a naive datetime.datetime in a given time zone aware."""
    if is_dst is NOT_PASSED:
        is_dst = None
    else:
        warnings.warn(
            "The is_dst argument to make_aware(), used by the Trunc() "
            "database functions and QuerySet.datetimes(), is deprecated as it "
            "has no effect with zoneinfo time zones.",
            RemovedInDjango50Warning,
        )
    if timezone is None:
        timezone = get_current_timezone()
    if _is_pytz_zone(timezone):
        # This method is available for pytz time zones.
        return timezone.localize(value, is_dst=is_dst)
    else:
        # Check that we won't overwrite the timezone of an aware datetime.
        if is_aware(value):
            # ADD THIS
            return value
            # REMOVE THE FOLLOWING LINE
            # raise ValueError("make_aware expects a naive datetime, got %s" % value)
        # This may be wrong around DST changes!
        return value.replace(tzinfo=timezone)

相关问题