mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-16 20:00:23 +00:00
Fix 'add_*_manager' signal handlers for Django 1.8+.
This commit is contained in:
parent
7012e16cc8
commit
3110794afc
1 changed files with 33 additions and 16 deletions
|
|
@ -36,6 +36,7 @@ class TimeFramedModel(models.Model):
|
|||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class StatusModel(models.Model):
|
||||
"""
|
||||
An abstract base class model with a ``status`` field that
|
||||
|
|
@ -51,6 +52,7 @@ class StatusModel(models.Model):
|
|||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
def add_status_query_managers(sender, **kwargs):
|
||||
"""
|
||||
Add a Querymanager for each status item dynamically.
|
||||
|
|
@ -59,16 +61,15 @@ def add_status_query_managers(sender, **kwargs):
|
|||
if not issubclass(sender, StatusModel):
|
||||
return
|
||||
for value, display in getattr(sender, 'STATUS', ()):
|
||||
try:
|
||||
sender._meta.get_field(value)
|
||||
raise ImproperlyConfigured("StatusModel: Model '%s' has a field "
|
||||
"named '%s' which conflicts with a "
|
||||
"status of the same name."
|
||||
% (sender.__name__, value))
|
||||
except FieldDoesNotExist:
|
||||
pass
|
||||
if _field_exists(sender, value):
|
||||
raise ImproperlyConfigured(
|
||||
"StatusModel: Model '%s' has a field named '%s' which "
|
||||
"conflicts with a status of the same name."
|
||||
% (sender.__name__, value)
|
||||
)
|
||||
sender.add_to_class(value, QueryManager(status=value))
|
||||
|
||||
|
||||
def add_timeframed_query_manager(sender, **kwargs):
|
||||
"""
|
||||
Add a QueryManager for a specific timeframe.
|
||||
|
|
@ -76,14 +77,12 @@ def add_timeframed_query_manager(sender, **kwargs):
|
|||
"""
|
||||
if not issubclass(sender, TimeFramedModel):
|
||||
return
|
||||
try:
|
||||
sender._meta.get_field('timeframed')
|
||||
raise ImproperlyConfigured("Model '%s' has a field named "
|
||||
"'timeframed' which conflicts with "
|
||||
"the TimeFramedModel manager."
|
||||
% sender.__name__)
|
||||
except FieldDoesNotExist:
|
||||
pass
|
||||
if _field_exists(sender, 'timeframed'):
|
||||
raise ImproperlyConfigured(
|
||||
"Model '%s' has a field named 'timeframed' "
|
||||
"which conflicts with the TimeFramedModel manager."
|
||||
% sender.__name__
|
||||
)
|
||||
sender.add_to_class('timeframed', QueryManager(
|
||||
(models.Q(start__lte=now) | models.Q(start__isnull=True)) &
|
||||
(models.Q(end__gte=now) | models.Q(end__isnull=True))
|
||||
|
|
@ -92,3 +91,21 @@ def add_timeframed_query_manager(sender, **kwargs):
|
|||
|
||||
models.signals.class_prepared.connect(add_status_query_managers)
|
||||
models.signals.class_prepared.connect(add_timeframed_query_manager)
|
||||
|
||||
|
||||
def _field_exists(model_class, field_name):
|
||||
if hasattr(model_class._meta, '_get_fields'):
|
||||
# Django 1.8+
|
||||
field_exists = bool([
|
||||
f for f in model_class._meta._get_fields(reverse=False)
|
||||
if f.name == field_name
|
||||
])
|
||||
else:
|
||||
# Django 1.7 and previous
|
||||
try:
|
||||
model_class._meta.get_field(field_name)
|
||||
except FieldDoesNotExist:
|
||||
field_exists = False
|
||||
else:
|
||||
field_exists = True
|
||||
return field_exists
|
||||
|
|
|
|||
Loading…
Reference in a new issue