我正在使用 django 1.4 和 python 2.7
我正在尝试从 MySQL 数据库中获取数据... View .py:
def myRequests(request):
#request = ProjectRequest.objects
response = render_to_response('myRequests.html', {'ProjectRequest': request}, context_instance = RequestContext(request))
return response
一旦我取消注释“request = ProjectRequest.objects”,我就会收到错误:
AttributeError at /myRequests/
'Manager' object has no attribute 'META'
我没有定义任何新的用户模型,所以这个错误对我来说毫无意义。 异常位置:
/{path}/lib/python2.7/site-packages/django/core/context_processors.py in debug, line 35
模型请求.py:
class ProjectRequest(Model):
reqType = CharField("Request Type", max_length = MAX_CHAR_LENGTH)
reqID = IntegerField("Request ID", primary_key = True)
ownerID = IntegerField("Owner ID")
projCodeName = CharField("Project Code Name", max_length = MAX_CHAR_LENGTH)
projPhase = CharField("Project Phase", max_length = MAX_CHAR_LENGTH)
projType = CharField("Project Phase", max_length = MAX_CHAR_LENGTH)
projNotes = CharField("Project Notes", max_length = MAX_CHAR_LENGTH)
contacts = CharField("Project Notes", max_length = MAX_CHAR_LENGTH)
reqStatus = CharField("Status", max_length = MAX_CHAR_LENGTH)
reqPriority = CharField("Request Priority", max_length = MAX_CHAR_LENGTH)
reqEstDate = DateTimeField("Request Estimated Complete Date", auto_now_add = True)
lastSaved = DateTimeField("Last saved", auto_now = True)
created = DateTimeField("Created", auto_now_add = True)
def __unicode__(self):
return str(self.reqID) + ": " + str(self.reqType)
class Meta:
app_label = 'djangoTestApp'
db_table = 'requests'
知道发生了什么吗?!
最佳答案
使用不同的变量名
project_request = ProjectRequest.objects
问题是因为这个 context_instance = RequestContext(request)
它丢失了 request
对象的上下文,因为它已被覆盖。因此问题。
def myRequests(request):
project_request = ProjectRequest.objects
#Now you have access to request object,
# do whatever you want with project_request -
response = render_to_response('myRequests.html', {'ProjectRequest': project_request}, context_instance = RequestContext(request))
return response
您收到错误 'Manager' object has no attribute 'META'
的原因是,当您这样做时
ProjectRequest.objects
模型 ProjectRequest
的默认管理器(objects
)被分配给(覆盖的)局部变量 request
。
关于mysql - Django 错误 : AttributeError at - 'Manager' object has no attribute 'META' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17415150/