Skip to content

Support ForeignKey relations with to keyword. Fixes #223 #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
* [bittner](https://github.com/bittner)
* [federicobond](https://github.com/federicobond)
* [matusvalo](https://github.com/matusvalo)
* [fadedDexofan](https://github.com/fadeddexofan)
20 changes: 17 additions & 3 deletions pylint_django/tests/input/func_noerror_foreignkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from django.db.models import ForeignKey, OneToOneField


class Genre(models.Model):
name = models.CharField(max_length=100)


class Author(models.Model):
author_name = models.CharField(max_length=100)

Expand All @@ -17,8 +21,10 @@ class ISBN(models.Model):

class Book(models.Model):
book_name = models.CharField(max_length=100)
author = models.ForeignKey('Author', on_delete=models.CASCADE)
isbn = models.OneToOneField(ISBN, on_delete=models.CASCADE)
# Check this works with and without `to` keyword
author = models.ForeignKey(to='Author', on_delete=models.CASCADE)
isbn = models.OneToOneField(to=ISBN, on_delete=models.CASCADE)
genre = models.ForeignKey(Genre, on_delete=models.CASCADE)

def get_isbn(self):
return self.isbn.value
Expand All @@ -32,7 +38,7 @@ class Fruit(models.Model):


class Seed(models.Model):
fruit = ForeignKey(Fruit, on_delete=models.CASCADE)
fruit = ForeignKey(to=Fruit, on_delete=models.CASCADE)

def get_fruit_name(self):
return self.fruit.fruit_name
Expand All @@ -56,3 +62,11 @@ class UserPreferences(models.Model):
https://github.com/PyCQA/pylint-django/issues/35
"""
user = ForeignKey('User', on_delete=models.CASCADE)


class UserAddress(models.Model):
user = OneToOneField(to='User', on_delete=models.CASCADE)
line_1 = models.CharField(max_length=100)
line_2 = models.CharField(max_length=100)
city = models.CharField(max_length=100)
postal_code = models.CharField(max_length=100)
7 changes: 6 additions & 1 deletion pylint_django/transforms/foreignkey.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import chain

from astroid import MANAGER, nodes, InferenceError, inference_tip, UseInferenceDefault
from astroid.nodes import ClassDef, Attribute

Expand All @@ -21,7 +23,10 @@ def is_foreignkey_in_class(node):


def infer_key_classes(node, context=None):
for arg in node.args:
keyword_args = [kw.value for kw in node.keywords]
all_args = chain(node.args, keyword_args)

for arg in all_args:
# typically the class of the foreign key will
# be the first argument, so we'll go from left to right
if isinstance(arg, (nodes.Name, nodes.Attribute)):
Expand Down