I have difficulties with the get_profile to link my Profile class with
the inbuilt User object.
The error message (which can be obtained through the 'chatroom' view
shown below, or via the shell) is:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call
last)
/Users/fangohr/local/hg/scico_web/debug/mysite/<ipython console> in
<module>()
/Library/Frameworks/Python.framework/Versions/4.0.30002/lib/python2.5/
site-packages/django/contrib/auth/models.pyc in get_profile(self)
291 app_label, model_name =
settings.AUTH_PROFILE_MODULE.split('.')
292 model = models.get_model(app_label, model_name)
--> 293 self._profile_cache =
model._default_manager.get(user__id__exact=self.id)
294 except (ImportError, ImproperlyConfigured):
295 raise SiteProfileNotAvailable
AttributeError: 'NoneType' object has no attribute '_default_manager'
This indicates that models.get_model() does return None, but I don't
know why.
I have in settings.py:
AUTH_PROFILE_MODULE = 'people.profile'
which I believe is the right entry.
For clarity, I have created a small django-site that can be downloaded
in a tar file as:
http://www.soton.ac.uk/~fangohr/geheim/django/get_profile/debug.tar.gz
or can be viewed online in the untarred version at
http://www.soton.ac.uk/~fangohr/geheim/django/get_profile/debug
I summarise the most important elements below (so that this email can
stand on its own for the archives):
mysite/People/models.py contains:
#-----------
from django.contrib.auth.models import User
from django.db import models
class Profile(models.Model):
user = models.ForeignKey(User,unique=True)
homepageURL=models.URLField('homepage',blank=True)
class Admin:
pass
#-----------
The corresponding view (which fails) is in mysite/People/views.py:
#-----------
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
def chatrooms(request):
u = User.objects.get(pk=1) # Get the first user
user_address = u.get_profile().homepageURL
#at this point we get an error, equivalent to shell example
#-----------
The mysite/settings.py reads
#-----------
# Django settings for mysite project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_...@domain.com'),
)
MANAGERS = ADMINS
DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2',
'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'test.dat' # Or path to database file if
using sqlite3.
DATABASE_USER = '' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost.
Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not
used with sqlite3.
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as
your
# system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as
not
# to load the internationalization machinery.
USE_I18N = True
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure
to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
AUTH_PROFILE_MODULE = 'people.profile'
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'afxb6gs$x!8o3z5+bc@4#g0^z_mpuscs1=#c700@cdpvn^&51@'
# List of callables that know how to import templates from various
sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
ROOT_URLCONF = 'mysite.urls'
#ROOT_URLCONF = 'urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/
django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'mysite.People',
'django.contrib.admin'
)
#------------
and the url.py is
#------------
from django.conf.urls.defaults import *
import mysite
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^mysite/$', 'mysite.People.views.chatrooms'),
(r'^admin/(.*)', admin.site.root),
)
#------------
The error can be triggered by viewing
or by running the shell example shown here:
phi:mysite fangohr$ python manage.py shell
Enthought Python Distribution -- http://code.enthought.com
Python 2.5.2 |EPD with Py2.5 4.0.30002 | (r252:60911, Oct 15 2008,
16:58:38)
Type "copyright", "credits" or "license" for more information.
IPython 0.9.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: from django.contrib.auth.models import User
In [2]: myuser=User.objects.all()[0]
In [3]: myuser.get_profile()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call
last)
/Users/fangohr/local/hg/scico_web/debug/mysite/<ipython console> in
<module>()
/Library/Frameworks/Python.framework/Versions/4.0.30002/lib/python2.5/
site-packages/django/contrib/auth/models.pyc in get_profile(self)
291 app_label, model_name =
settings.AUTH_PROFILE_MODULE.split('.')
292 model = models.get_model(app_label, model_name)
--> 293 self._profile_cache =
model._default_manager.get(user__id__exact=self.id)
294 except (ImportError, ImproperlyConfigured):
295 raise SiteProfileNotAvailable
AttributeError: 'NoneType' object has no attribute '_default_manager'
In [4]:
I have checked other entries on the mailing list, but couldn't find
the solution to this problem.
Any help is very welcome.
Many thanks,
Hans
PS Version:
phi:mysite fangohr$ python manage.py --version
1.0.2 final
PPS In the database for this example, the admin user is 'admin' and
the password is 'admin' -- in case anybody wants to play with that.
Karen
You are right: using 'People.profile' works. To be clear (for others): the application name should (in contrast to the documentation) NOT be converted to lowercase. (But the actual model -- here 'Profile' -- should.)Then everything works.Should I report this? If so, could you point me in the right direction, please?
Thanks,
Karen