Search code examples
mysqldjangomodelsdjango-managers

Django error: AttributeError at - 'Manager' object has no attribute 'META'


I'm using django 1.4, and python 2.7

I'm trying to get data from the MySQL database... views.py:

def myRequests(request):
    #request = ProjectRequest.objects
    response = render_to_response('myRequests.html', {'ProjectRequest': request}, context_instance = RequestContext(request))
    return response

As soon as I uncomment 'request = ProjectRequest.objects' I get the error:

AttributeError at /myRequests/
'Manager' object has no attribute 'META'

I'm not defining any new user models so this error makes no sense to me. Exception location:

/{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'

Any idea what's going on?!


Solution

  • Use a different variable name

    project_request = ProjectRequest.objects
    

    The issue is because of this context_instance = RequestContext(request)

    It loses context of the request object as it has been overwritten. Hence the issue.

    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
    

    The reason you are getting the error 'Manager' object has no attribute 'META' is, when you do

    ProjectRequest.objects
    

    the default manager (objects) for models ProjectRequest is assign to the (overwritten) local variable request.