Skip to content

Commit

Permalink
Support django 2.2+ in ExtendedQuerySet
Browse files Browse the repository at this point in the history
See also: jazzband#107
  • Loading branch information
tony committed Jun 19, 2019
1 parent 6ec1350 commit 54fac92
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions django_celery_monitor/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from celery import states
from celery.events.state import Task
from celery.utils.time import maybe_timedelta
import django
from django.db import models, router, transaction

from .utils import Now
Expand All @@ -26,13 +27,30 @@ def select_for_update_or_create(self, defaults=None, **kwargs):
select_for_update when getting the object.
"""
defaults = defaults or {}
lookup, params = self._extract_model_params(defaults, **kwargs)
self._for_write = True

if django.VERSION < (2,2):
lookup, params = self._extract_model_params(defaults, **kwargs)
with transaction.atomic(using=self.db):
try:
obj = self.select_for_update().get(**lookup)
except self.model.DoesNotExist:
obj, created = self._create_object_from_params(lookup, params)
if created:
return obj, created
for k, v in defaults.items():
setattr(obj, k, v() if callable(v) else v)
obj.save(using=self.db)
return obj, False

with transaction.atomic(using=self.db):
try:
obj = self.select_for_update().get(**lookup)
obj = self.select_for_update().get(**kwargs)
except self.model.DoesNotExist:
obj, created = self._create_object_from_params(lookup, params)
params = self._extract_model_params(defaults, **kwargs)
# Lock the row so that a concurrent update is blocked until
# after update_or_create() has performed its save.
obj, created = self._create_object_from_params(kwargs, params, lock=True)
if created:
return obj, created
for k, v in defaults.items():
Expand Down

0 comments on commit 54fac92

Please sign in to comment.