Python 给定导入错误,即使是I';我已经导入模型类了吗?
我有一个像这样的模特Python 给定导入错误,即使是I';我已经导入模型类了吗?,python,django,django-models,Python,Django,Django Models,我有一个像这样的模特 from notifications.models import SignupInfoEmail class Request(models.Model): created = models.DateTimeField(auto_now_add=True, editable=False) modified = models.DateTimeField(auto_now=True, editable=False) las
from notifications.models import SignupInfoEmail
class Request(models.Model):
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
last_contacted = models.DateTimeField(editable=False, null=True, blank=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
phone = models.CharField(max_length=30,null=True, blank=True)
zipcode = models.CharField(max_length=5,null=True, blank=True)
def __unicode__(self):
return str(self.email)
def send_info(self):
first_name = self.first_name
price = self.plan().calculate_price()
#from notifications.models import SignupInfoEmail
if self.status.upper() == "NEW":
self.status = "info sent"
self.last_contacted = datetime.datetime.now()
self.save()
return SignupInfoEmail.objects.send_info(self, self.email, first_name, price, self.info_url())
当我调用send_info函数时,它正在提供导入错误:即使我已经从通知模型中导入了SignupInfoEmail,也无法导入name SignupInfoEmail。我尝试从文件顶部删除导入语句,并在send_info函数(注释语句)中编写导入语句,然后工作正常。为什么?原因是什么?您具有循环依赖性;可能notification.models正在导入此模型文件。是的,Daniel,我正在notification.models中导入此model.py。那么,是否有任何解决方案可以将所有这些导入语句保持在models.py的顶部?@vijaykumark
import
是一个可执行语句,在第一次为当前进程导入模块时执行(以及所有其他顶级可执行语句,包括class
和def
)。这意味着您不能让两个模块相互导入(至少在两个模块的顶层都有import
语句)。