Joel G Mathew
Joel G Mathew

Reputation: 8061

Unable to import Model defined in another app

I have two apps appointments and clinic in my project myappointments. I have defined a model in appointments/models.py:

class Clinicdb(models.Model):
    clinicid = models.AutoField(primary_key=True, unique=True)
    name = models.CharField(max_length=60, unique=True)
    label = models.SlugField(max_length=25, unique=True)
    email = models.EmailField(max_length=50, default='')
    mobile = models.CharField(max_length=15, default='')
    alternate = models.CharField(max_length=15, default='', blank=True)
    about = models.CharField(max_length=250, blank=True)
    state = models.CharField(max_length=25)
    city = models.CharField(max_length=35)
    locality = models.CharField(max_length=35)
    pincode = models.IntegerField(default=0)
    address = models.TextField(max_length=80, default='', blank=True)
    website = models.URLField(blank=True)
    logo = models.ForeignKey(ProfilePic, blank=True, null=True, on_delete=models.CASCADE)

    class Meta:        
        unique_together = ["name", "mobile", "email"]

    def __str__(self):
        return self.name

In clinic/models.py, I have:

from appointments.models import Clinicdb
class Album (models.Model):
    name = models.CharField(max_length=255)
    photo = models.FileField(upload_to="data/media/%Y/%m/%d")
    clinicid = models.ForeignKey(Clinicdb, blank=True,
                             null=True, on_delete=models.CASCADE)
    def __str__(self):
        return self.photo

However when running makemigrations (and runserver) I get:

joel@hp:~/myappointments$ python3 manage.py makemigrations 
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/joel/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/joel/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/home/joel/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/joel/.local/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/home/joel/.local/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/joel/myappointments/appointments/models.py", line 4, in <module>
    from clinic.models import Pic, ProfilePic
  File "/home/joel/myappointments/clinic/models.py", line 3, in <module>
    from appointments.models import Clinicdb
ImportError: cannot import name 'Clinicdb'

Upvotes: 1

Views: 77

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476547

You made a cyclic import, if we look at the bottom of the traceback, we see that:

File "/home/joel/myappointments/appointments/models.py", line 4, in <module>
  from clinic.models import Pic, ProfilePic
File "/home/joel/myappointments/clinic/models.py", line 3, in <module>
  from appointments.models import Clinicdb

So that means that clinic/models.py needs to import appointments/models.py first, but vice versa also holds, hence we can not import this. It comes down to the "chicken and the egg" problem: for an egg, we need a chicken, but for a chicken we need an agg, which results in a logical problem.

What you thus need to do is remove the import, and use a qualified name in a string, like:

# clinic/models.py

# NO import

class Album (models.Model):
    name = models.CharField(max_length=255)
    photo = models.FileField(upload_to="data/media/%Y/%m/%d")
    clinicid = models.ForeignKey('appointment.Clinicdb', blank=True,
                             null=True, on_delete=models.CASCADE)
    def __str__(self):
        return self.photo

Django will automatically load the INSTALLED_APPS and replace the qualified string with a reference to the model (in fact one of the reasons of this functionality is solve the circular reference problem).

Note: normally ForeignKeys (and related relation fields) do not end with an id, or _id suffix, so I suggest that you rename clinicid to clinic. This makes sense since a some_album.clinic is not an id of a Clinic, but a Clinic object. Django will automatically add an extra field named clinic_id that stores the id.

Upvotes: 3

Related Questions