Skip to content

Commit

Permalink
Fix Django 1.9 deprecation warnings
Browse files Browse the repository at this point in the history
* replaced get_model with haystack_get_model which returns the right function depending on the Django version
* get_haystack_models is now compliant with > Django 1.7

Closes #1206
  • Loading branch information
Koed00 authored and acdha committed Jun 8, 2015
1 parent 6098526 commit ad90028
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -103,3 +103,4 @@ Thanks to
* Alfredo Armanini (@phingage) for a patch fixing compatibility with database API changes in Django 1.8
* Ben Spaulding (@benspaulding) for many updates for Django 1.8 support
* Troy Grosfield (@troygrosfield) for fixing the test runner for Django 1.8
* Ilan Steemers (@Koed00) for fixing Django 1.9 deprecation warnings
4 changes: 2 additions & 2 deletions haystack/backends/elasticsearch_backend.py
Expand Up @@ -8,7 +8,6 @@

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models.loading import get_model
from django.utils import six

import haystack
Expand All @@ -19,6 +18,7 @@
from haystack.models import SearchResult
from haystack.utils import log as logging
from haystack.utils import get_identifier, get_model_ct
from haystack.utils.app_loading import haystack_get_model

try:
import elasticsearch
Expand Down Expand Up @@ -597,7 +597,7 @@ def _process_results(self, raw_results, highlight=False,
source = raw_result['_source']
app_label, model_name = source[DJANGO_CT].split('.')
additional_fields = {}
model = get_model(app_label, model_name)
model = haystack_get_model(app_label, model_name)

if model and model in indexed_models:
for key, value in source.items():
Expand Down
4 changes: 2 additions & 2 deletions haystack/backends/solr_backend.py
Expand Up @@ -6,7 +6,6 @@

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models.loading import get_model
from django.utils import six

from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery, EmptyResults, log_query
Expand All @@ -16,6 +15,7 @@
from haystack.models import SearchResult
from haystack.utils import log as logging
from haystack.utils import get_identifier, get_model_ct
from haystack.utils.app_loading import haystack_get_model

try:
from pysolr import Solr, SolrError
Expand Down Expand Up @@ -378,7 +378,7 @@ def _process_results(self, raw_results, highlight=False, result_class=None, dist
for raw_result in raw_results.docs:
app_label, model_name = raw_result[DJANGO_CT].split('.')
additional_fields = {}
model = get_model(app_label, model_name)
model = haystack_get_model(app_label, model_name)

if model and model in indexed_models:
index = unified_index.get_index(model)
Expand Down
4 changes: 2 additions & 2 deletions haystack/backends/whoosh_backend.py
Expand Up @@ -10,7 +10,6 @@

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models.loading import get_model
from django.utils import six
from django.utils.datetime_safe import datetime

Expand All @@ -21,6 +20,7 @@
from haystack.models import SearchResult
from haystack.utils import log as logging
from haystack.utils import get_identifier, get_model_ct
from haystack.utils.app_loading import haystack_get_model

try:
import json
Expand Down Expand Up @@ -609,7 +609,7 @@ def _process_results(self, raw_page, highlight=False, query_string='', spelling_
score = raw_page.score(doc_offset) or 0
app_label, model_name = raw_result[DJANGO_CT].split('.')
additional_fields = {}
model = get_model(app_label, model_name)
model = haystack_get_model(app_label, model_name)

if model and model in indexed_models:
for key, value in raw_result.items():
Expand Down
29 changes: 16 additions & 13 deletions haystack/utils/app_loading.py
Expand Up @@ -4,18 +4,14 @@
from django import VERSION as DJANGO_VERSION
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models.loading import get_app, get_model, get_models

from haystack.utils import importlib


__all__ = ['haystack_get_models', 'haystack_load_apps']


APP = 'app'
MODEL = 'model'


if DJANGO_VERSION >= (1, 7):
from django.apps import apps

Expand All @@ -29,19 +25,22 @@ def haystack_load_apps():

def haystack_get_models(label):
try:
app_mod = get_app(label)
if app_mod is not None:
return get_models(app_mod=app_mod)
app_mod = apps.get_app_config(label)
return app_mod.get_models()
except LookupError:
if '.' not in label:
raise ImproperlyConfigured('Unknown application label {}'.format(label))
app_label, model_name = label.rsplit('.', 1)
return [apps.get_model(app_label, model_name)]
except ImproperlyConfigured:
pass

if '.' not in label:
raise ImproperlyConfigured("No installed application has the label %s" % label)

app_label, model_name = label.rsplit('.', 1)
return [get_model(app_label, model_name)]
def haystack_get_model(app_label, model_name):
return apps.get_model(app_label, model_name)

else:
from django.db.models.loading import get_app, get_model, get_models

def is_app_or_model(label):
label_bits = label.split('.')

Expand All @@ -54,7 +53,8 @@ def is_app_or_model(label):
return APP
return MODEL
else:
raise ImproperlyConfigured("'%s' isn't recognized as an app (<app_label>) or model (<app_label>.<model_name>)." % label)
raise ImproperlyConfigured(
"'%s' isn't recognized as an app (<app_label>) or model (<app_label>.<model_name>)." % label)

def haystack_get_app_modules():
"""Return the Python module for each installed app"""
Expand Down Expand Up @@ -85,3 +85,6 @@ def haystack_get_models(label):
else:
app_label, model_name = label.rsplit('.', 1)
return [get_model(app_label, model_name)]

def haystack_get_model(app_label, model_name):
return get_model(app_label, model_name)

0 comments on commit ad90028

Please sign in to comment.