Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added lock helper to model instance #95

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion chamber/importers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ def import_rows(self, reader, row_count=0):
batch.append(self.model_class(**self.get_fields_dict(row)))
created += self.create_batch(batch)
self._post_batch_create(len(batch), row_count)

self._post_import_rows(created)

return created
Expand Down
12 changes: 11 additions & 1 deletion chamber/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from distutils.version import StrictVersion

import django
from django.db import models, transaction
from django.db import models, transaction, OperationalError
from django.db.models.base import ModelBase
from django.utils.translation import ugettext_lazy as _
from django.utils.functional import cached_property
Expand Down Expand Up @@ -519,6 +519,16 @@ def change_and_save(self, update_only_changed_fields=False, **changed_fields):
change_and_save(self, update_only_changed_fields=update_only_changed_fields, **changed_fields)
return self

def get_locked_instance(self):
"""
Lock object and reload it from database.
:return: reloaded locked object from database
"""
if not self.pk:
raise OperationalError('Unsaved object cannot be locked')

return self.__class__.objects.filter(pk=self.pk).select_for_update().get()

class Meta:
abstract = True

Expand Down