无法导入在其他应用程序中定义的模型

时间:2018-09-21 10:50:00

标签: django django-models

我的项目myappointments中有两个应用程序约会和诊所。 我在约会/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

在临床/模型.py中,我有:

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

但是,在运行makemigrations(和runserver)时,我得到了:

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'

1 个答案:

答案 0 :(得分:3)

您进行了循环导入,如果我们查看回溯的底部,则会看到:

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

因此,这意味着clinic/models.py首先需要导入appointments/models.py,但反之亦然,因此我们不能导入它。归结为“ 鸡肉和鸡蛋”问题:对于一个鸡蛋,我们需要一只鸡,但是对于一个鸡肉,我们需要一个鸡蛋,这导致了逻辑上的问题。

因此,您需要做的是删除导入,并在字符串中使用限定名称,例如:

# 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将自动 加载INSTALLED_APPS并将合格的字符串替换为对模型的引用(实际上,此功能的原因之一是解决了循环引用问题)。 / p>

  

注意:通常ForeignKey(及相关关系字段)id_id后缀结尾,因此建议您将clinicid重命名为clinic。这是有道理的,因为some_album.clinic不是id的{​​{1}}而是Clinic对象的Clinic。 Django将自动添加一个名为clinic_id的额外字段来存储id