【问题标题】:Django import issue when updating models更新模型时的 Django 导入问题
【发布时间】:2017-10-16 10:17:20
【问题描述】:

我在更新我的一个模型后使用导入时遇到问题我收到错误 ImportError: cannot import name 'Team'

这是我的实际模型

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)

我想将 candidat_answer 从 Response 模型添加到 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' 

初始化

"""
    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"]

【问题讨论】:

    标签: django django-models django-views


    【解决方案1】:

    听起来你有一个循环导入。如果仅在外键中使用模型,则无需导入模型。删除导入并改用字符串'<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')
    

    【讨论】:

    • 感谢 Alasdair 为您解答,当我尝试时,我得到 ImportError: cannot import name 'Response'。当有问题的模型是响应时,我的模型位于 response.py 中的文件夹模型中
    • 我无法从错误消息中判断出问题所在。完整的回溯可能会显示问题所在。
    猜你喜欢
    • 2011-08-06
    • 1970-01-01
    • 2011-05-21
    • 2020-07-11
    • 1970-01-01
    • 2013-04-02
    • 2022-06-14
    相关资源
    最近更新 更多