简体   繁体   English

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

[英]ImportError: cannot import name and Cannot import class

I got a full code from github and everything was OK, server worked without any problems. 我从github获得了完整的代码,一切正常,服务器正常工作。 But then I tried to do some changes, like adding a new class in the model.py and trying to import it to the admin.py I got such an error: 但是后来我尝试做一些更改,例如在model.py添加一个新类,然后尝试将其导入到admin.py我得到了这样的错误:

ImportError: cannot import name TechnicalExamination. ImportError:无法导入名称TechnicalExamination。

Of course, I did migrations before this, using python manage.py makemigrations and python manage.py migrate . 当然,在此之前,我使用python manage.py makemigrationspython manage.py migrate

Here is my class in models.py: 这是我在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)

Here is my admin.py: 这是我的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)

And here is my root: 这是我的根源:

在此处输入图片说明

  1. Check migration file - you should have operations contain CreateModel your TechnicalExamination 检查迁移文件-您应该使操作包含CreateModel的技术考试

     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 如果迁移文件有问题,请删除迁移目录并再次进行迁移(您可能还需要清除数据库)或使用--fake

  2. import from django shell: 从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 尝试通过from destrict_office.models import TechnicalExamination

I experienced some problems with .models import method, and it's not clear import method. 我在使用.models导入方法时遇到了一些问题, .models尚不清楚导入方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM