Skip to content

Commit

Permalink
Rely on django for module name resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-angulo committed Jun 24, 2020
1 parent 3afe91e commit 2c4ad3e
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions pylint_django/transforms/foreignkey.py
Expand Up @@ -87,10 +87,28 @@ def infer_key_classes(node, context=None):
module_name = current_module.name
elif not module_name.endswith('models'):
# otherwise Django allows specifying an app name first, e.g.
# ForeignKey('auth.User') so we try to convert that to
# 'auth.models', 'User' which works nicely with the `endswith()`
# comparison below
module_name += '.models'
# ForeignKey('auth.User')

supported_django_version_installed = False
try:
import django
supported_django_version_installed = (django.VERSION[0] == 3)
except ImportError:
pass

if supported_django_version_installed:
# If Django is installed we can use it to resolve the module name
from django.apps import apps

app = app.get_app_config(module_name)
model = app.get_model(model_name)
module_name = model.__module__
else:
# Otherwise we try to convert that to
# 'auth.models', 'User' which works nicely with the `endswith()`
# comparison below
module_name += '.models'

# ensure that module is loaded in astroid_cache, for cases when models is a package
if module_name not in MANAGER.astroid_cache:
MANAGER.ast_from_module_name(module_name)
Expand Down

0 comments on commit 2c4ad3e

Please sign in to comment.