我正在使用django 1.4和python 2.7
我正在尝试从MySQL数据库中获取数据...... views.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
modelsRequest.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'
知道发生了什么事吗?!
答案 0 :(得分:2)
使用其他变量名称
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
模型objects
的默认管理器(ProjectRequest
)被分配给(覆盖的)局部变量request
。