TypeError: __init__() got an unexpected keyword argument 'export_job'

TypeError: __init__() got an unexpected keyword argument 'export_job'

我正在使用 django-import-export-celeryinstructions 实现导出功能 只有三个步骤。我遵循了所有这些,当我尝试导出时,它在 celery 过程中给我一个错误:

我的代码是这样的:

class Issuer(models.Model):
    name = models.CharField(max_length=200, null=False, blank=False,)

    @classmethod
    def export_resource_classes(cls):
        return {
            "Issuers": ("Issuers resource", IssuerResource),
        }


class IssuerResource(ModelResource):
    def ready(self):
        class Meta:
            model = apps.get_model('crowdfunding.Issuer')

感谢任何帮助。

更新:添加堆栈跟踪作为文本。

[2022-03-07 05:02:21,061: INFO/MainProcess] Task py_scripts.background_check.export_sdn_results[2ca53746-c134-4bb5-a35b-ccd27e1a845d] succeeded in 0.0s: None
[2022-03-07 05:28:01,005: INFO/MainProcess] Task import_export_celery.tasks.run_export_job[bcc37ee4-258e-4a98-881d-8b7ccc9487c4] received
[2022-03-07 05:28:01,006: INFO/MainProcess] import_export_celery.tasks.run_export_job[bcc37ee4-258e-4a98-881d-8b7ccc9487c4]: Exporting 1
[2022-03-07 05:28:01,082: ERROR/MainProcess] Task import_export_celery.tasks.run_export_job[bcc37ee4-258e-4a98-881d-8b7ccc9487c4] raised unexpected: TypeError("Resource.__init__() got an unexpected keyword argument 'export_job'")
Traceback (most recent call last):
  File "C:\Users\Windows 10\AppData\Local\Programs\Python\Python310\lib\site-packages\celery\app\trace.py", line 450, in trace_task
    R = retval = fun(*args, **kwargs)
  File "C:\Users\Windows 10\AppData\Local\Programs\Python\Python310\lib\site-packages\sentry_sdk\integrations\celery.py", line 200, in _inner
    reraise(*exc_info)
  File "C:\Users\Windows 10\AppData\Local\Programs\Python\Python310\lib\site-packages\sentry_sdk\_compat.py", line 54, in reraise
    raise value
  File "C:\Users\Windows 10\AppData\Local\Programs\Python\Python310\lib\site-packages\sentry_sdk\integrations\celery.py", line 195, in _inner
    return f(*args, **kwargs)
  File "C:\Users\Windows 10\AppData\Local\Programs\Python\Python310\lib\site-packages\celery\app\trace.py", line 731, in __protected_call__
    return self.run(*args, **kwargs)
  File "C:\Users\Windows 10\AppData\Local\Programs\Python\Python310\lib\site-packages\import_export_celery\tasks.py", line 219, in run_export_job
    resource = Resource(export_job=export_job)
  File "C:\Users\Windows 10\AppData\Local\Programs\Python\Python310\lib\site-packages\import_export_celery\tasks.py", line 207, in __init__
    super().__init__(*args, **kwargs)
TypeError: Resource.__init__() got an unexpected keyword argument 'export_job'

更新 2:添加了新的 ModelResource 和堆栈跟踪,更新了 django-import-export-celery 版本 1.1.6

新模型资源:

class IssuerResource(ModelResource):
    class Meta:
        model = Issuer

新堆栈跟踪:

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Program Files\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Program Files\lib\site-packages\sentry_sdk\integrations\threading.py", line 69, in run
    reraise(*_capture_exception())
  File "C:\Program Files\lib\site-packages\sentry_sdk\_compat.py", line 54, in reraise
    raise value
  File "C:\Program Files\lib\site-packages\sentry_sdk\integrations\threading.py", line 67, in run
    return old_run_func(self, *a, **kw)
  File "C:\Program Files\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Program Files\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Program Files\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "C:\Program Files\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "C:\Program Files\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Program Files\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Program Files\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Program Files\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Program Files\lib\site-packages\django\apps\config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\Prosy\Desktop\dalmore\dalmoreportal\misc\models.py", line 1, in <module>
    from crowdfunding.submodels.issuer_model import Issuer
  File "C:\Users\Prosy\Desktop\dalmore\dalmoreportal\crowdfunding\submodels\__init__.py", line 1, in <module>
    from .issuer_model import *
  File "C:\Users\Prosy\Desktop\dalmore\dalmoreportal\crowdfunding\submodels\issuer_model.py", line 78, in <module>
    class IssuerResource(ModelResource):
  File "C:\Program Files\lib\site-packages\import_export\resources.py", line 926, in __new__
    field = new_class.field_from_django_field(f.name, f,
  File "C:\Program Files\lib\site-packages\import_export\resources.py", line 1075, in field_from_django_field
    FieldWidget = cls.widget_from_django_field(django_field)
  File "C:\Program Files\lib\site-packages\import_export\resources.py", line 1041, in widget_from_django_field
    result = getattr(cls, result)(f)
  File "C:\Program Files\lib\site-packages\import_export\resources.py", line 1012, in get_m2m_widget
    model=get_related_model(field))
  File "C:\Program Files\lib\site-packages\import_export\resources.py", line 45, in get_related_model
    if hasattr(field, 'related_model'):
  File "C:\Program Files\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Program Files\lib\site-packages\django\db\models\fields\related.py", line 95, in related_model
    apps.check_models_ready()
  File "C:\Program Files\lib\site-packages\django\apps\registry.py", line 141, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

您必须进行迁移。在您进行迁移之前,Django 无法识别“export_job”。

这已在 django-import-export-celery 1.1.5 及更高版本中修复。

等效代码修复:

class IssuerResource(ModelResource):
    def __init__(self, *args, **kwargs):   # +
        kwargs.pop('export_job', None)     # +
        super().__init__(*args, **kwargs)  # +

    class Meta:
        model = Issuer

参考:django-import-export-celery@5fde9d0