Artur Drożdżyk
Artur Drożdżyk

Reputation: 615

Django: Changing record's value before deleting it

I am trying to change one of the record's field before deleting it:

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

The point is that written above code causes an exception:

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

I have tried playing with

with transaction.atomic():

statement, but it did not change anything at all.

Moreover, following combination:

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

still causes the same error

@edit

in the entire project there is only one signal pointed on records belonging to the model of query_item:

pre_delete.connect(create_receipts, sender=itemToPurchase)

@edit2

It definitely is something wrong with the pre_delete signal

I just removed it and the test went smooth and fine.

Anyway, whenever I am using written above signal & delete each try-except statement, I do receive following error log:

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:

Upvotes: 0

Views: 44

Answers (1)

H&#229;ken Lid
H&#229;ken Lid

Reputation: 23064

I can't give you a solution. But the traceback shows that there's an error in the signal handler function. Specifically this line:

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

Django handles both pre_delete, post_delete and the deletion in an atomic database transaction. So if there is a bug in any of those steps, no changes are commited to the database. This prevents that your data ends up in a limbo because you deleted something, but a bug prevented some other cleanup or other.

To fix the bug, you should look for some code that causes this import error.

ModuleNotFoundError: No module named 'app_namespace'

This is related to Django's template loading framework. I suppose there's some template rendering happening in the create_receipts signal handler function, and django fails to load the module app_namespace to find the template file.

app_namespace looks like a placeholder name. If you search your code base for this variable name, you might find the source of the bug. It might be a combination of an incorrect template name and a nonexistant module name or directory in your django template loading config in settings.py.

Upvotes: 1

Related Questions