首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Django:在删除前更改记录的值

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

Stack Overflow用户
提问于 2018-10-09 02:53:50
回答 1查看 28关注 0票数 0

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

代码语言:javascript
复制
query_item.txId = txId
try:
    query_item.delete()
except Exception as e:
        print(str(e))

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

当前事务中发生错误。您不能执行查询,直到‘原子’块的末尾。

我试着玩过

代码语言:javascript
复制
with transaction.atomic():

语句,但它根本没有改变任何事情。

此外,还包括以下组合:

代码语言:javascript
复制
query_item.txId = txId
query_item.save()
try:
    query_item.delete()
except Exception as e:
        print(str(e))

仍然会导致相同的错误

@编辑

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

代码语言:javascript
复制
pre_delete.connect(create_receipts, sender=itemToPurchase)

@edit2

这肯定是pre_delete信号出了问题

我只是移除了它,测试进行得很顺利。

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

代码语言:javascript
复制
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:
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-09 03:26:40

我不能给你一个解决方案。但是回溯显示信号处理函数中有一个错误。特别是这一行:

代码语言:javascript
复制
File "User/profilespaidbybtc/receipt/models.py", line 186, in create_receipts
  'token': hash_generator_token.make_token(instance_receiptModel)

Django在一个原子数据库事务中同时处理pre_deletepost_delete和删除。因此,如果这些步骤中的任何一个步骤中存在错误,都不会向数据库提交任何更改。这可以防止您的数据因为您删除了某些内容而最终处于不确定状态,但bug阻止了其他清理或其他操作。

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

代码语言:javascript
复制
ModuleNotFoundError: No module named 'app_namespace'

这与Django的模板加载框架有关。我认为在create_receipts信号处理函数中发生了一些模板呈现,并且django无法加载模块app_namespace来查找模板文件。

app_namespace看起来像一个占位符名称。如果您在代码库中搜索此变量名称,您可能会找到错误的来源。这可能是错误的模板名称和settings.py中django模板加载配置中不存在的模块名称或目录的组合。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52708451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档