Django:无法访问模型类
上的object属性我正忙着构建一个Django App,它从数据库中返回一组需求。然而,我在View.py中得到了ab ObjectDoesNotExist异常。我正在使用Visual Studio 2015.我将非常感谢您解决此错误的所有帮助。
以下是Models.py中的代码:
from django.db import models
class Requirement_Matrix(models.Model):
RequirementName = models.CharField(max_length=128)
在Views.py中,Visual Studio 2015中的intellisense表示对象(第12行)是未知类型。当你运行程序时,它会引发一个对象不存在异常。
01 from django.core.exceptions import ObjectDoesNotExist
02 from ScopeManagement.models import Requirement_Matrix
03
04
05 def index(request):
06 # Query the database for a list of requirements for a specific project.
07 # Place the list in our Requirement_dict dictionary which will be passed to the
08 #template engine.
09
10 try:
11
12 Requirement_list = Requirement_Matrix.objects.all() ### <- Visual Studio 2015
13 # is saying that objects is unknown type and then it raises an object does
14 # not exist exception
15
16 Requirement_dict = {'requirements': Requirement_list}
17
18 except ObjectDoesNotExist:
19 print ('Does Not Exist!')
20
21 # Render the response and send it back
22 return render(request, 'ScopeManagement/index.html', Requirement_dict)
但是,当您通过python manage.py shell查询模型时,它确实返回一个值:
python manage.py shell
>>> from ScopeManagement.models import Requirement_Matrix
>>> Requirement_Matrix.objects.all()
[<Requirement_Matrix: Requirement 1>]
可能是什么原因?