Python and Django models in the same file do not see each other

class B(models.Model):
    whatever = models.TextField(blank=True)

    @staticmethod
    def are_we_ok():
        return False

class A(models.Model)
    text = models.TextField(blank=True)

    @staticmethod
    def is_everything_ok():
        if not B.are_we_ok():
            raise B.DoesNotExist




A.is_everything_ok()

Why am I getting the error:

File "asdf/models.py", line x, in is_everything_ok
    if not B.are_we_ok():

AttributeError: 'NoneType' object has no attribute 'are_we_ok'

However, if I do this:

class A(models.Model)
    text = models.TextField(blank=True)

    @staticmethod
    def is_everything_ok():
        from asdf.models import B
        if not B.are_we_ok():
            raise B.DoesNotExist

it works. That makes no sense to me. This is part of the huge Django app. Any ideas on what situation might cause this? (Is circular dependence possible, for example?)

Update:

What I forgot to mention is that this code has been working for four years in production without any problems. Only some recent unrelated changes caused this error.

+4
source share
2 answers

The reason for this was circular imports. I reorganized models for models instead of the massive models.py file and was able to get rid of this.

, Django/Python ​​ "". , Python - Java, Java.

0

@staticmethod @classmethod. staticmethod self , .

, :

class B(models.Model):
    whatever = models.TextField(blank=True)

    @classmethod
    def are_we_ok(cls):
        return False

: @staticmethod @classmethod Python? .

+1

Source: https://habr.com/ru/post/1529416/


All Articles