Attribute Error in Django model Foreign Key

23 views
Skip to first unread message

Pradyum Gupta

unread,
Sep 24, 2020, 5:00:36 AM9/24/20
to django-res...@googlegroups.com

In My Django Project, there are two apps: Login and Company

 

The **error that am receiving** in this is

```python

AttributeError: module 'login.models' has no attribute 'Country'

```

 

*Company App* > models.py

```python

from django.db import models

from login import models as LM

 

class CompanyProfile(models.Model):

    full_name            = models.CharField(max_length=255,

                                            unique = True)

 

    country              = models.ForeignKey(LM.Country,

                                            on_delete=models.SET_NULL,

                                            null=True,

                                            blank=False)

 

    state                = models.ForeignKey(LM.State,

                                            on_delete=models.SET_NULL,

                                            null=True,

                                            blank=False)

    def __str__(self):

        return self.full_name

```

 

*Login App* > models.py

````python

 

class Country(models.Model):

    """List of Country"""

    name = models.CharField(max_length=50, unique= True, default='None')

    code = models.CharField(max_length=2, unique= True, primary_key=True, default ='NA')

 

    def __str__(self):

        return str(self.code)

 

class State(models.Model):

    """List fo State"""

    region = models.CharField(max_length = 255, unique = True, primary_key=True, default='None')

    country = models.ForeignKey(Country, on_delete=models.SET_NULL, null=True, blank=False, default ='NA')

 

    def __str__(self):

        return self.region

 

````

 

Here is test to check that weather is login is getting imported or not

```python

def test_import():

    try:

        # import pdb; pdb.set_trace()

        importlib.find_loader('LM.Country')

        found = True

    except ImportError:

        found = False

    print(found)

```

 

Answer is received stands to be True

```

python3 manage.py shell

>>> test_import()

True

```

 

Now on other stackoverflow blogs i checked i thought it could be of ```Circlular Import```

But i have already fixed that still am getting this error?

 

Thanks in Advance

Regards

 

PS: I have also asked the same question here https://stackoverflow.com/q/64042995/7999665

 

 

naveen Kumar

unread,
Sep 24, 2020, 5:37:24 AM9/24/20
to django-res...@googlegroups.com
i thinks it will be helpful if you place screenshots of you project  file structure as you mentioned along with models, so that people can easily trace out the issue.....

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/SG2PR04MB27612A4B2E7020F7576BF114A7390%40SG2PR04MB2761.apcprd04.prod.outlook.com.
Reply all
Reply to author
Forward
0 new messages