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

时间:2018-10-08 18:53:50

标签: django database record

我正尝试在删除记录的字段之一之前将其删除:

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

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

  

当前事务中发生错误。你不能执行   直到“ atomic”块结束为止。

我尝试过

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:

1 个答案:

答案 0 :(得分:1)

我无法给您解决方案。但是回溯表明信号处理程序函数中存在错误。特别是这一行:

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模板加载配置中,可能是模板名称不正确和模块名称或目录不存在的组合。