我從github獲得了完整的代碼,一切正常,服務器無任何問題地工作。但後來我試圖做一些改變,像model.py
添加一個新的類,並試圖將其導入到admin.py
我得到了這樣的錯誤:ImportError:無法導入名稱,也無法導入類
ImportError: cannot import name TechnicalExamination.
當然,我在此之前沒有遷移,使用python manage.py makemigrations
和python 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)
這裏是我的根:
聽起來像'TechnicalExamination'沒有定義在與其他類相同的水平。 –
TechnicalExamination在models.py上,就像HealthExaminations一樣。但第一個不起作用,第二個起作用。 – Viola
當然,但是'def __str__'在問題中沒有正確縮進,那麼您能否在文件中至少顯示兩個模型?一個工作,那個? –