Search code examples
pythondjangodjango-modelspylint

pylint_django "Instance of 'ForeignKey' has no ... member"


I'm working on a project in python with django and recently I decided to modify the way the models were stored in one of the apps. With the help of django documentation, I deleted the models.py file and created a models folder in which I put all my models in files and created a _init_.py file in which I import all my models. This solution works but now pylint no longer seems to understand ForeignKeys.

this is my app tree:

  • apps

    • myapp

      • models

        • _init_.py

        • mymodel.py

        • mymodel2.py

if i run pylint i can see some E1101. let's take an example:

i have a model subnet in subnet.py:

class Subnet(models.Model):
    def __str__(self):
        return self.nom + "(" + self.cidr + ")"

    net = models.ForeignKey("Network", on_delete=models.CASCADE, related_name="subnets")

    def get_parent_cidr(self):
        return self.net.cidr

this model have a reference to the network models in network.py:

class Network(models.Model):
    def __str__(self):
        return self.company.name + " - " + self.nom + " - " + str(self.cidr)
    cidr = models.CharField(max_length=18)

this network object have a cidr field, all seems to be fine but when i run pylint with the pylint_django plugin, this one send me an error:

project/apps/myapp/models/subnet.py:74:15: E1101: Instance of 'ForeignKey' has no 'cidr' member (no-member)

What did I do wrong and how can I correct this error?


Solution

  • I've added ForeignKey to ignored-classes under [TYPECHECK]. It worked. pylint-django doesn't understands self.company.name.