ImportError:无法导入名称而无法导入类

时间:2017-12-18 00:41:45

标签: python django import

我从github获得了一个完整的代码,一切都很好,服务器工作没有任何问题。但后来我尝试做一些更改,比如在model.py中添加一个新类并尝试将其导入admin.py我收到了这样的错误:

  

ImportError:无法导入名称TechnicalExamination。

当然,我之前使用python manage.py makemigrationspython manage.py migrate进行了迁移。

这是我在models.py中的课程:

class HealthExamination(models.Model):
class Meta:
    verbose_name_plural = 'Health Examinations'

doctor = models.CharField(max_length=70)
person = models.ForeignKey(Person, on_delete=models.CASCADE)
start_date = models.DateField()
end_date = models.DateField()

class TechnicalExamination(models.Model):
  class Meta:
    verbose_name_plural = 'Technical Examinations'

  technician = models.CharField(max_length=70)
  person = models.ForeignKey(Person, on_delete=models.CASCADE)
  start_date = models.DateField()
  end_date = models.DateField()

def get_fields(self):
    pairs = []
    for field in self._meta.fields:
        name = field.name
        try:
            pairs.append((name, getattr(self, "get_%s_display" % name)()))
        except AttributeError:
            pairs.append((name, getattr(self, name)))
    return pairs

def __str__(self):
  return str(self.person)

这是我的admin.py:

from __future__ import unicode_literals

from django.contrib import admin

from .models import Person, Car, InsuranceCompany, Policy,   HealthExamination, TechnicalExamination

admin.site.register(Person)
admin.site.register(Car)
admin.site.register(InsuranceCompany)
admin.site.register(Policy)
admin.site.register(HealthExamination)
admin.site.register(TechnicalExamination)

这是我的根:

enter image description here

1 个答案:

答案 0 :(得分:0)

  1. Check migration file - you should have operations contain CreateModel your TechnicalExamination

    operations = [
        migrations.CreateModel(
            name='TechnicalExamination',
    ...
    

    If something wrong with migration files, remove migrations directory and makemigration again (You might need to clear your database , too) or use --fake

  2. import from django shell:

    $python manage.py shell
    > from your_app_name.models import TechnicalExamination
    

If not imported, you can see more detail error messages and debug it.

  1. Try import by from destrict_office.models import TechnicalExamination

I experienced some problems with .models import method, and it's not clear import method.