1

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

The error that am receiving in this is

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

Company App > models.py

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


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

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

2
  • Can u try : country = models.ForeignKey(LM.Country, on_delete=models.SET_NULL, null=True, blank=False, default ='NA'). in State Commented Sep 24, 2020 at 9:24
  • Here is the resultstate = models.ForeignKey(LM.Country, AttributeError: module 'login.models' has no attribute 'Country' Commented Sep 24, 2020 at 9:32

1 Answer 1

1

I am not able to see any issue here technically. Maybe Django doesn't support this alias way of mentioning model as Foreign Key which I have never tried this way.

But I would suggest to use string format for adding Foreign Key of other model as below.

class CompanyProfile(models.Model):
    full_name = models.CharField(max_length=255, unique = True)
    # In following line, as I mention model name in string which django understands
    country = models.ForeignKey('login.Country', on_delete=models.SET_NULL, 
                                null=True,blank=False)

Another way is simple import but it might be a problem in case of circular depedencies. So I don't recommend to use that.

I hope you get the answer out of it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.