更新模型时Django导入问题

时间:2017-10-16 10:17:21

标签: django django-models django-views

在更新我的某个模型后使用导入时遇到问题我收到错误导入错误:无法导入名称'团队'

这是我的实际模型

from django.db import models
from registration.models import MyUser
from django.core.urlresolvers import reverse

# Create your models here.

class Team(models.Model):
    team_name = models.CharField(max_length=100, default = '')
    team_hr_admin = models.ForeignKey(MyUser, blank=True, null=True)
    members = models.ManyToManyField(MyUser, related_name="members")

    def __str__(self):
        return self.team_name


class Project(models.Model):
    name = models.CharField(max_length=250)
    team_id = models.ForeignKey(Team, blank=True, null=True)
    project_hr_admin = models.ForeignKey(MyUser, blank=True, null=True)

我想从Response模型中将candidat_answer添加到Project模型中,所以我导入了模型以便能够使用它,这给了我实际的代码:

from django.db import models
from registration.models import MyUser
from survey.models.response import Response
from django.core.urlresolvers import reverse

# Create your models here.

class Team(models.Model):
    team_name = models.CharField(max_length=100, default = '')
    team_hr_admin = models.ForeignKey(MyUser, blank=True, null=True)
    members = models.ManyToManyField(MyUser, related_name="members")

    def __str__(self):
        return self.team_name


class Project(models.Model):
    name = models.CharField(max_length=250)
    team_id = models.ForeignKey(Team, blank=True, null=True)
    project_hr_admin = models.ForeignKey(MyUser, blank=True, null=True)
    candidat_answers = models.ForeignKey(Response)

但是现在我从注册导入视图中收到导入错误..

from website.models import Team, Project
ImportError: cannot import name 'Team' 

init

"""
    Permit to import everything from survey.models without knowing the details.
"""
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

from future import standard_library
standard_library.install_aliases()
import sys

from .answer import Answer
from .category import Category
from .question import Question
from .response import Response
from .survey import Survey


__all__ = ["Category", "Answer", "Category", "Response", "Survey", "Question"]

1 个答案:

答案 0 :(得分:2)

听起来你有一个循环导入。如果您只在外键中使用它们,则不需要导入模型。删除导入并改为使用字符串'<app_name>.<Model name>',例如:

class Project(models.Model):
    name = models.CharField(max_length=250)
    team_id = models.ForeignKey(Team, blank=True, null=True)
    project_hr_admin = models.ForeignKey('registration.MyUser', blank=True, null=True)
    candidat_answers = models.ForeignKey('survey.Response')