Skip to content

Commit 186b47f

Browse files
author
Tommy Wu
committed
Make the ForeiginKey detection more accurate
To handle this case: the project use tastypie and django. tastypie has a `ForeignKey` field which has the same name as django's `ForeignKey`. The issue is the lint trys resolving the `ForeignKey` for the tastypie `ForeignKey` which cause import error. In this commit, add a check to ensure the current class of the `ForeignKey` is a subclass of `Model` of django. Tested manually
1 parent f4f609e commit 186b47f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

pylint_django/transforms/foreignkey.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def is_foreignkey_in_class(node):
1616
if not isinstance(node.parent.parent, ClassDef):
1717
return False
1818

19+
is_in_django_model_class = False
20+
# Make sure the class is the subclass of django.db.models.Model
21+
for ancestor in node.parent.parent.ancestors():
22+
if ancestor.name == 'Model':
23+
is_in_django_model_class = True
24+
break
25+
if not is_in_django_model_class:
26+
return False
27+
1928
if isinstance(node.func, Attribute):
2029
attr = node.func.attrname
2130
elif isinstance(node.func, nodes.Name):

0 commit comments

Comments
 (0)