Django - no attribute _default_manager
17, Apr 2012
I recently deployed a new version of my web site here after changing hosts to A Small Orange after gettting exasperated with GoDaddy's support for Python and Django (where my interests have moved after working on To Be Done 3). During the deploy, I came across an error that I could not duplicate on my development machine. I'm not sure what caused the error: both are running the same version of Django (1.3.1), however, on different versions of Python (the error showed on 2.6.4).
On the 'Add' page of the admin site for a specific model I was getting the following error:
AttributeError at /admin/<model>/post/add 'str' object has no attribute '_default_manager'
The traceback was not very useful, as not a single entry in it pointed to any of my own code everything was a part of the Django core codebase. Some Googling showed me that several other people had experienced the same problem, though it seemed that the error appeared for different reasons in some cases - it appeared to be a generic error.
After some more looking around, I found a comment in the depths of the internet saying that a common cause of this error is that there is an object that is not getting translated properly from a String to an instance of said Object. With that in mind I looked through the models.py code for the app in question and found the following line:
class Post(models.Model): ... image = models.ForeignKey('core.image', related_name='post', blank=True, null=True) ....
My actual model in the core app is Image. As suggested, it appears the image model was not actually getting converted to the object when it came time to load the model forms.
Fixing the issue was as simple as changing that the case on core.image to core.Image, so that it now looks like:
image = models.ForeignKey('core.Image', related_name='post', blank=True, null=True)
The only remaining mystery is why that relationship worked on all the other pages. Ah well, that's for someone else to figure out.
Make sure that your naming of models when referencing Foreign Keys are in correct case.
Have Something to Say?
Questions? Comments? Concerns? Let me know what you’re thinking.