django 2.2(含 drf)sqlite3 转成 mysql5.7 报错:return instance._state.fields_cache[cache_name] KeyError: 'article'(已解决)

导出导入过程如下:

1. settings DATABASES 使用 sqlite3,执行:

python manage.py dumpdata > data.json

 导出 sqlite3 数据。

2. settings DATABASES 使用 mysql, 执行:

python manage.py migrate

 生成数据表,然后要删除已经生成数据的表:

3. 进入到MySQL,执行如下的 SQL 语句,删除已经生成数据的表:

use 你的数据库名;
delete from auth_permission;
delete from django_content_type;

4.settings DATABASES 使用 mysql, 执行:

python manage.py loaddata data.json

 

django 2.2(含 drf) sqlite3 转成 mysql5.7 ,执行 python manage.py loaddata data.json 时报错如下(有一个 article 模型):

(joyoo) [root@VM_2_29_centos blog]# python manage.py loaddata data.json 
/root/.virtualenvs/joyoo/lib/python3.6/site-packages/daphne/server.py:15: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.
  UserWarning,
System check identified some issues:

Traceback (most recent call last):
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 164, in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/fields/mixins.py", line 13, in get_cached_value
    return instance._state.fields_cache[cache_name]
KeyError: 'article'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
    self.loaddata(fixture_labels)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 114, in loaddata
    self.load_label(fixture_label)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 181, in load_label
    obj.save(using=self.using)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/core/serializers/base.py", line 223, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/base.py", line 790, in save_base
    update_fields=update_fields, raw=raw, using=using,
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/root/yzq/djangos/blog/blog/blog_signals.py", line 88, in change_comment
    comments = instance.article.comment_article_set.filter(is_delete=False).order_by(
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 178, in __get__
    rel_obj = self.get_object(instance)
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 145, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
  File "/root/.virtualenvs/joyoo/lib/python3.6/site-packages/django/db/models/query.py", line 408, in get
    self.model._meta.object_name
blog.models.DoesNotExist: Problem installing fixture '/root/yzq/djangos/blog/data.json': Article matching query does not exist.
Sentry is attempting to send 1 pending error messages
Waiting up to 10 seconds
Press Ctrl-C to quit

   

同类问题:

解决办法:

把 app 里面启动信号的地方注释掉导入就能成功,导入完成后再重新打开信号。

  1 from django.apps import AppConfig
  2 
  3 
  4 class BlogConfig(AppConfig):
  5     name = 'blog'
  6     verbose_name = "B 博客管理"
  7 
  8     # 启用信号
  9     #def ready(self):
 10    #    import blog.blog_signals

  内存不足失败的日志(2H2G 服务器导入失败,这个是内存不足,被系统干掉)

(joyoo) [root@VM_2_29_centos blog]# python manage.py loaddata /root/yzq/download/data.json
/root/.virtualenvs/joyoo/lib/python3.6/site-packages/daphne/server.py:15: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.
  UserWarning,
System check identified some issues:

WARNINGS:
finance.FundNet.fund: (fields.W342) Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.
        HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
Building prefix dict from the default dictionary ...
jieba: 2022-04-12 01:35:45,402 /root/.virtualenvs/joyoo/lib/python3.6/site-packages/jieba/__init__.py [line:111] DEBUG Building prefix dict from the default dictionary ...
Loading model from cache /tmp/jieba.cache
jieba: 2022-04-12 01:35:45,406 /root/.virtualenvs/joyoo/lib/python3.6/site-packages/jieba/__init__.py [line:131] DEBUG Loading model from cache /tmp/jieba.cache
Loading model cost 1.018 seconds.
jieba: 2022-04-12 01:35:46,420 /root/.virtualenvs/joyoo/lib/python3.6/site-packages/jieba/__init__.py [line:163] DEBUG Loading model cost 1.018 seconds.
Prefix dict has been built succesfully.
jieba: 2022-04-12 01:35:46,434 /root/.virtualenvs/joyoo/lib/python3.6/site-packages/jieba/__init__.py [line:164] DEBUG Prefix dict has been built succesfully.
Killed

  导入成功的日志(在本地台式机导入的)

(joyo) E:\git_stores\blog>python manage.py loaddata data.json
E:\py_envs\joyo\lib\site-packages\daphne\server.py:15: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.
  UserWarning,
System check identified some issues:

WARNINGS:
finance.FundNet.fund: (fields.W342) Setting unique=True on a ForeignKey has the same effect as using a OneToOneField.
        HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
Building prefix dict from the default dictionary ...
jieba: 2023-02-27 23:00:59,499 E:\py_envs\joyo\lib\site-packages\jieba\__init__.py [line:111] DEBUG Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\yinzh\AppData\Local\Temp\jieba.cache
jieba: 2023-02-27 23:00:59,501 E:\py_envs\joyo\lib\site-packages\jieba\__init__.py [line:131] DEBUG Loading model from cache C:\Users\yinzh\AppData\Local\Temp\jieba.cache
Loading model cost 0.836 seconds.
jieba: 2023-02-27 23:01:00,336 E:\py_envs\joyo\lib\site-packages\jieba\__init__.py [line:163] DEBUG Loading model cost 0.836 seconds.
Prefix dict has been built succesfully.
jieba: 2023-02-27 23:01:00,337 E:\py_envs\joyo\lib\site-packages\jieba\__init__.py [line:164] DEBUG Prefix dict has been built succesfully.
Installed 561367 object(s) from 1 fixture(s)

  

posted @ 2022-02-17 16:44  yinzhuoqun  阅读(214)  评论(0编辑  收藏  举报