2014-06-18 41 views
0

我有一個項目,有3個應用程序,稱爲項目,標本和測試。這工作正常,直到我做出改變(我不記得是什麼)。之後,Django拋出ImportError: No module named models.我無法想象是什麼問題,因爲我認爲我已經很好地引用和組織了。突然,沒有名爲模型的模塊

這是我的錯誤:

ImportError at/

No module named models 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/ 
Django Version:  1.5.5 
Exception Type:  ImportError 
Exception Value:  

No module named models 

Exception Location:  /home/david/mysqldb/dev/mysqldb/teams_db/specimens/views.py in <module>, line 11 
Python Executable: /usr/bin/python 
Python Version:  2.7.5 

,這是我回溯:

/home/david/mysqldb/dev/mysqldb/teams_db/teams_db/urls.py in <module> 

     (r'^specimens/', include('specimens.urls')), 

▶ Local vars 
/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py in include 

      urlconf_module = import_module(urlconf_module) 

▶ Local vars 
/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py in import_module 

     __import__(name) 

▶ Local vars 
/home/david/mysqldb/dev/mysqldb/teams_db/specimens/urls.py in <module> 

    from views import new_specimen, list_specimens, info_specimen 

▶ Local vars 
/home/david/mysqldb/dev/mysqldb/teams_db/specimens/views.py in <module> 

    from tests.models import Test 

▶ Local vars 

應用tests我有類測試的內部,即引起錯誤的一個。這是引發錯誤(specimens.views)功能:

from django.shortcuts import render 
from django.http import HttpResponse, HttpResponseRedirect,\ 
         HttpResponseForbidden, HttpResponseBadRequest,\ 
         HttpResponseServerError 
from django.core.urlresolvers import reverse 
from django.template import RequestContext 
from django.shortcuts import render_to_response, get_object_or_404 
from forms import SearchForm, SpecimenForm 
from projects.models import Project, Serie 
from models import Specimen 
from tests.models import Test ## Test importation!! 
import simplejson 
import json 
from django.core import serializers 
from django.forms.models import model_to_dict 
import datetime 


def new_specimen(request, project_id=None, serie_id=None): 
    ... 

而我的測試類tests.models:

from django.db import models 
from projects.models import Project 
from specimens.models import Specimen 
#from operator.models import Operator 
from django.core.validators import RegexValidator 

class Test(models.Model): 

    ref   = models.CharField(max_length=70, unique=True) 
    #name  = models.CharField(max_length=70, null=True, blank=True, default='NULL') 
    project  = models.ForeignKey(Project) 
    specimen = models.ForeignKey(Specimen) 
    #operator = models.ForeignKey(Operator) 
    notes  = models.TextField(max_length=170, blank=True) 
    description = models.TextField(max_length=170, blank=True) 
    start_date = models.DateField(null=True, blank=True) 
    finish_date = models.DateField(null=True, blank=True) 
    status  = models.CharField(max_length=70, blank=True) 

你需要更多信息?請,如果有必要讓我知道。任何想法的問題?可能是那個名字tests是保留字?

在此先感謝!

+0

你是否試圖打開你的python shell並單獨導入'tests'模塊?如果你能夠做到這一點,然後尋找它的路徑:'import tests; print tests .__ file__'。也許這不是你所期望的。 – Germano

+2

你有循環依賴。您沒有足夠的代碼來確切知道它在哪裏,但是tests.models正在導入projects.models和specimen.models,並且很可能其中一個導入了tests.models,因此是導入循環。 –

+0

請檢查它:'python manage.py shell'。然後'從tests.models導入Test'。它會返回什麼?它是好還是不好? – Mortezaipo

回答

0

它看起來像一個導入循環,但我懷疑你在其他地方導入Test模型。不要在您的視圖from models import Specimen中使用相對導入,它可能會導致您的問題。並且不要在您的tests.py中放置任何型號,它只能用於測試,您不需要任何測試模型進行測試。