Django:在删除记录之前更改记录的值

Django: Changing record's value before deleting it

我正在尝试在删除记录之前更改其中一个字段:

query_item.txId = txId
try:
    query_item.delete()
except Exception as e:
        print(str(e))

关键是上面写的代码导致了异常:

An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

我试过

with transaction.atomic():

声明,但它根本没有改变任何东西。

此外,还有以下组合:

query_item.txId = txId
query_item.save()
try:
    query_item.delete()
except Exception as e:
        print(str(e))

仍然会导致同样的错误

@edit

整个项目中只有一个信号指向属于query_item型号的记录:

pre_delete.connect(create_receipts, sender=itemToPurchase)

@edit2

肯定是pre_delete信号有问题

我刚删除它,测试很顺利。

无论如何,每当我使用上面写的信号并删除每个 try-except 语句时,我都会收到以下错误日志:

Traceback (most recent call last):
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 74, in inner
    return func(*args, **kwds)
  File "User/env/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "User/env/lib/python3.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "User/profilespaidbybtc/payment_realizer/views.py", line 240, in post
    query_item.delete()
  File "User/env/lib/python3.7/site-packages/django/db/models/base.py", line 880, in delete
    return collector.delete()
  File "User/env/lib/python3.7/site-packages/django/db/models/deletion.py", line 277, in delete
    sender=model, instance=obj, using=self.using
  File "User/env/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "User/env/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "User/profilespaidbybtc/receipt/models.py", line 186, in create_receipts
    'token': hash_generator_token.make_token(instance_receiptModel)
  File "User/env/lib/python3.7/site-packages/django/template/loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "User/env/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
    template, origin = self.find_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 124, in find_template
    for loader in self.template_loaders:
  File "User/env/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 98, in template_loaders
    return self.get_template_loaders(self.loaders)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 103, in get_template_loaders
    loader = self.find_template_loader(template_loader)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 116, in find_template_loader
    loader_class = import_string(loader)
  File "User/env/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "User/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'app_namespace'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "User/env/lib/python3.7/site-packages/django/utils/deprecation.py", line 91, in __call__
    response = response or self.get_response(request)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "User/env/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "User/env/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
    template = loader.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
    template, origin = self.find_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 124, in find_template
    for loader in self.template_loaders:
  File "User/env/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 98, in template_loaders
    return self.get_template_loaders(self.loaders)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 103, in get_template_loaders
    loader = self.find_template_loader(template_loader)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 116, in find_template_loader
    loader_class = import_string(loader)
  File "User/env/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "User/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'app_namespace'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "User/env/lib/python3.7/site-packages/django/utils/deprecation.py", line 91, in __call__
    response = response or self.get_response(request)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "User/env/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "User/env/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
    template = loader.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
    template, origin = self.find_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 124, in find_template
    for loader in self.template_loaders:
  File "User/env/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 98, in template_loaders
    return self.get_template_loaders(self.loaders)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 103, in get_template_loaders
    loader = self.find_template_loader(template_loader)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 116, in find_template_loader
    loader_class = import_string(loader)
  File "User/env/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "User/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'app_namespace'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "User/env/lib/python3.7/site-packages/django/utils/deprecation.py", line 91, in __call__
    response = response or self.get_response(request)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "User/env/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "User/env/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
    template = loader.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
    template, origin = self.find_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 124, in find_template
    for loader in self.template_loaders:
  File "User/env/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 98, in template_loaders
    return self.get_template_loaders(self.loaders)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 103, in get_template_loaders
    loader = self.find_template_loader(template_loader)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 116, in find_template_loader
    loader_class = import_string(loader)
  File "User/env/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "User/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'app_namespace'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "User/env/lib/python3.7/site-packages/django/utils/deprecation.py", line 91, in __call__
    response = response or self.get_response(request)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "User/env/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "User/env/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
    template = loader.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
    template, origin = self.find_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 124, in find_template
    for loader in self.template_loaders:
  File "User/env/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 98, in template_loaders
    return self.get_template_loaders(self.loaders)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 103, in get_template_loaders
    loader = self.find_template_loader(template_loader)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 116, in find_template_loader
    loader_class = import_string(loader)
  File "User/env/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "User/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'app_namespace'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "User/env/lib/python3.7/site-packages/django/utils/deprecation.py", line 91, in __call__
    response = response or self.get_response(request)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "User/env/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "User/env/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
    template = loader.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
    template, origin = self.find_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 124, in find_template
    for loader in self.template_loaders:
  File "User/env/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 98, in template_loaders
    return self.get_template_loaders(self.loaders)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 103, in get_template_loaders
    loader = self.find_template_loader(template_loader)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 116, in find_template_loader
    loader_class = import_string(loader)
  File "User/env/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "User/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'app_namespace'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "User/env/lib/python3.7/site-packages/django/utils/deprecation.py", line 91, in __call__
    response = response or self.get_response(request)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "User/env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "User/env/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "User/env/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
    template = loader.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
    template, origin = self.find_template(template_name)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 124, in find_template
    for loader in self.template_loaders:
  File "User/env/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 98, in template_loaders
    return self.get_template_loaders(self.loaders)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 103, in get_template_loaders
    loader = self.find_template_loader(template_loader)
  File "User/env/lib/python3.7/site-packages/django/template/engine.py", line 116, in find_template_loader
    loader_class = import_string(loader)
  File "User/env/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "User/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'app_namespace'

During handling of the above exception, another exception occurred:

我无法给你解决方案。但是回溯显示信号处理函数中存在错误。特别是这一行:

File "User/profilespaidbybtc/receipt/models.py", line 186, in create_receipts
  'token': hash_generator_token.make_token(instance_receiptModel)

Django 处理 pre_deletepost_delete 和原子数据库事务中的删除。因此,如果任何这些步骤中存在错误,则不会向数据库提交任何更改。这可以防止你的数据因为你删除了一些东西而陷入困境,但是一个错误阻止了一些其他的清理或其他。

要修复此错误,您应该查找导致此导入错误的代码。

ModuleNotFoundError: No module named 'app_namespace'

这个和Django的模板加载框架有关。我想 create_receipts 信号处理函数中发生了一些模板渲染,django 无法加载模块 app_namespace 来查找模板文件。

app_namespace 看起来像一个占位符名称。如果您在代码库中搜索此变量名称,您可能会找到错误的来源。它可能是 settings.py.

中的 django 模板加载配置中不正确的模板名称和不存在的模块名称或目录的组合