Skip to content

Commit

Permalink
Merge pull request #23 from seawolf42/fix/base-db-queries-on-apps-ready
Browse files Browse the repository at this point in the history
Fix/base db queries on apps ready
  • Loading branch information
seawolf42 committed Jun 16, 2018
2 parents cda4ea3 + 3c1b424 commit 4d8e344
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lookup_tables/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@

from django.conf import settings


_SETTINGS = getattr(settings, 'LOOKUP_TABLES', {})

USE_ADMIN_SORTABLE2 = _SETTINGS.get('USE_ADMIN_SORTABLE2', False)
DRF_REPRESENTATION_NAME_NOT_ID = _SETTINGS.get('DRF_REPRESENTATION_NAME_NOT_ID', False)

IS_RUNNING_MANAGEMENT = len(sys.argv) > 1 and sys.argv[0].endswith('manage.py') and sys.argv[1] != 'runserver'

_IGNORE_INIT_RESET_KEY = 'LOOKUP_TABLES_DRF_FIELD_INIT_NO_RESET'

if _IGNORE_INIT_RESET_KEY in os.environ:
IGNORE_INIT_RESET = os.environ[_IGNORE_INIT_RESET_KEY].lower() in ('1', 'true')
elif len(sys.argv) > 1 and sys.argv[0].endswith('manage.py') and sys.argv[1] != 'runserver':
elif IS_RUNNING_MANAGEMENT:
IGNORE_INIT_RESET = True
else:
IGNORE_INIT_RESET = False
Expand Down
3 changes: 2 additions & 1 deletion lookup_tables/drf_fields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from lookup_tables.models import LookupTableItem
from rest_framework import fields
import six

from lookup_tables.models import LookupTableItem

from . import conf


Expand Down
7 changes: 7 additions & 0 deletions lookup_tables/fields.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from django.apps import apps
from django.db import models as db_models

from . import conf
from . import models


_IGNORE_FIELD_DEFAULT_LOOKUPS = conf.IS_RUNNING_MANAGEMENT


class LookupTableItemField(db_models.ForeignKey):

def __init__(self, table_ref, *args, **kwargs):
Expand Down Expand Up @@ -30,6 +35,8 @@ def get_lookuptableitem_choices(self):
return db_models.Q(table=self.lookuptable)

def get_default_lookuptableitem(self):
if _IGNORE_FIELD_DEFAULT_LOOKUPS or not apps.ready:
return None
if not self.lookuptable:
self._init_lookuptable()
return models.LookupTableItem.objects.filter(table__table_ref=self.table_ref, is_default=True).first()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

setup(
name='django-lookup-tables',
version='0.13.0',
version='0.13.1',
packages=find_packages(),
include_package_data=True,
license='MIT License',
Expand Down

0 comments on commit 4d8e344

Please sign in to comment.