7 | | I have difficulties with the `get_profile` to link my `Profile` class with |
8 | | the inbuilt User object. |
9 | | |
10 | | The error message (which can be obtained through the 'chatroom' view |
11 | | shown below, or via the shell) is: |
12 | | |
13 | | {{{ |
14 | | --------------------------------------------------------------------------- |
15 | | AttributeError Traceback (most recent call last) |
16 | | |
17 | | /Users/fangohr/local/hg/scico_web/debug/mysite/<ipython console> in <module>() |
18 | | |
19 | | /Library/Frameworks/Python.framework/Versions/4.0.30002/lib/python2.5/site-packages/django/contrib/auth/models.pyc in get_profile(self) |
20 | | 291 app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.') |
21 | | 292 model = models.get_model(app_label, model_name) |
22 | | --> 293 self._profile_cache = model._default_manager.get(user__id__exact=self.id) |
23 | | 294 except (ImportError, ImproperlyConfigured): |
24 | | 295 raise SiteProfileNotAvailable |
25 | | |
26 | | AttributeError: 'NoneType' object has no attribute '_default_manager' |
27 | | }}} |
28 | | |
29 | | This indicates that `models.get_model()` does return `None`, but I don't know why. |
30 | | |
31 | | I have in `settings.py`: |
32 | | {{{ |
33 | | AUTH_PROFILE_MODULE = 'people.profile' |
34 | | }}} |
35 | | which I believe is the right entry. |
36 | | |
37 | | |
38 | | For clarity, I have created a small django-site that can be downloaded in a tar file as: |
39 | | |
40 | | http://www.soton.ac.uk/~fangohr/geheim/django/get_profile/debug.tar.gz |
41 | | |
42 | | or can be viewed online in the untarred version at |
43 | | |
44 | | http://www.soton.ac.uk/~fangohr/geheim/django/get_profile/debug |
45 | | |
46 | | I summarise the most important elements below (so that this email can stand on its own for the archives): |
47 | | |
48 | | |
49 | | `mysite/People/models.py` contains: |
50 | | {{{ |
51 | | #----------- |
52 | | from django.contrib.auth.models import User |
53 | | from django.db import models |
54 | | |
55 | | class Profile(models.Model): |
56 | | user = models.ForeignKey(User,unique=True) |
57 | | homepageURL=models.URLField('homepage',blank=True) |
58 | | |
59 | | class Admin: |
60 | | pass |
61 | | #----------- |
62 | | }}} |
63 | | |
64 | | The corresponding view (which fails) is in `mysite/People/views.py`: |
65 | | {{{ |
66 | | #----------- |
67 | | from django.contrib.auth.models import User |
68 | | from django.contrib.auth.decorators import login_required |
69 | | |
70 | | |
71 | | def chatrooms(request): |
72 | | u = User.objects.get(pk=1) # Get the first user |
73 | | user_address = u.get_profile().homepageURL |
74 | | #at this point we get an error, equivalent to shell example |
75 | | #----------- |
76 | | }}} |
77 | | The `mysite/settings.py` reads |
78 | | {{{ |
79 | | #----------- |
80 | | # Django settings for mysite project. |
81 | | |
82 | | DEBUG = True |
83 | | TEMPLATE_DEBUG = DEBUG |
84 | | |
85 | | ADMINS = ( |
86 | | # ('Your Name', 'your_email@domain.com'), |
87 | | ) |
88 | | |
89 | | MANAGERS = ADMINS |
90 | | |
91 | | DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. |
92 | | DATABASE_NAME = 'test.dat' # Or path to database file if using sqlite3. |
93 | | DATABASE_USER = '' # Not used with sqlite3. |
94 | | DATABASE_PASSWORD = '' # Not used with sqlite3. |
95 | | DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. |
96 | | DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. |
97 | | |
98 | | # Local time zone for this installation. Choices can be found here: |
99 | | # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name |
100 | | # although not all choices may be available on all operating systems. |
101 | | # If running in a Windows environment this must be set to the same as your |
102 | | # system time zone. |
103 | | TIME_ZONE = 'America/Chicago' |
104 | | |
105 | | # Language code for this installation. All choices can be found here: |
106 | | # http://www.i18nguy.com/unicode/language-identifiers.html |
107 | | LANGUAGE_CODE = 'en-us' |
108 | | |
109 | | SITE_ID = 1 |
110 | | |
111 | | # If you set this to False, Django will make some optimizations so as not |
112 | | # to load the internationalization machinery. |
113 | | USE_I18N = True |
114 | | |
115 | | # Absolute path to the directory that holds media. |
116 | | # Example: "/home/media/media.lawrence.com/" |
117 | | MEDIA_ROOT = '' |
118 | | |
119 | | # URL that handles the media served from MEDIA_ROOT. Make sure to use a |
120 | | # trailing slash if there is a path component (optional in other cases). |
121 | | # Examples: "http://media.lawrence.com", "http://example.com/media/" |
122 | | MEDIA_URL = '' |
123 | | |
124 | | # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a |
125 | | # trailing slash. |
126 | | # Examples: "http://foo.com/media/", "/media/". |
127 | | ADMIN_MEDIA_PREFIX = '/media/' |
128 | | |
129 | | AUTH_PROFILE_MODULE = 'people.profile' |
130 | | |
131 | | # Make this unique, and don't share it with anybody. |
132 | | SECRET_KEY = 'afxb6gs$x!8o3z5+bc@4#g0^z_mpuscs1=#c700@cdpvn^&51@' |
133 | | |
134 | | # List of callables that know how to import templates from various sources. |
135 | | TEMPLATE_LOADERS = ( |
136 | | 'django.template.loaders.filesystem.load_template_source', |
137 | | 'django.template.loaders.app_directories.load_template_source', |
138 | | # 'django.template.loaders.eggs.load_template_source', |
139 | | ) |
140 | | |
141 | | MIDDLEWARE_CLASSES = ( |
142 | | 'django.middleware.common.CommonMiddleware', |
143 | | 'django.contrib.sessions.middleware.SessionMiddleware', |
144 | | 'django.contrib.auth.middleware.AuthenticationMiddleware', |
145 | | ) |
146 | | |
147 | | ROOT_URLCONF = 'mysite.urls' |
148 | | #ROOT_URLCONF = 'urls' |
149 | | |
150 | | TEMPLATE_DIRS = ( |
151 | | # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". |
152 | | # Always use forward slashes, even on Windows. |
153 | | # Don't forget to use absolute paths, not relative paths. |
154 | | ) |
155 | | |
156 | | INSTALLED_APPS = ( |
157 | | 'django.contrib.auth', |
158 | | 'django.contrib.contenttypes', |
159 | | 'django.contrib.sessions', |
160 | | 'django.contrib.sites', |
161 | | 'mysite.People', |
162 | | 'django.contrib.admin' |
163 | | ) |
164 | | #------------ |
165 | | }}} |
166 | | and the `url.py` is |
167 | | {{{ |
168 | | #------------ |
169 | | from django.conf.urls.defaults import * |
170 | | import mysite |
171 | | |
172 | | from django.contrib import admin |
173 | | admin.autodiscover() |
174 | | |
175 | | urlpatterns = patterns('', |
176 | | (r'^mysite/$', 'mysite.People.views.chatrooms'), |
177 | | (r'^admin/(.*)', admin.site.root), |
178 | | ) |
179 | | #------------ |
180 | | }}} |
181 | | |
182 | | The error can be triggered by viewing |
183 | | |
184 | | http://localhost:8000/mysite/ |
185 | | |
186 | | or by running the shell example shown here: |
187 | | |
188 | | {{{ |
189 | | phi:mysite fangohr$ python manage.py shell |
190 | | Enthought Python Distribution -- http://code.enthought.com |
191 | | |
192 | | Python 2.5.2 |EPD with Py2.5 4.0.30002 | (r252:60911, Oct 15 2008, 16:58:38) |
193 | | Type "copyright", "credits" or "license" for more information. |
194 | | |
195 | | IPython 0.9.1 -- An enhanced Interactive Python. |
196 | | ? -> Introduction and overview of IPython's features. |
197 | | %quickref -> Quick reference. |
198 | | help -> Python's own help system. |
199 | | object? -> Details about 'object'. ?object also works, ?? prints more. |
200 | | |
201 | | In [1]: from django.contrib.auth.models import User |
202 | | |
203 | | In [2]: myuser=User.objects.all()[0] |
204 | | |
205 | | In [3]: myuser.get_profile() |
206 | | --------------------------------------------------------------------------- |
207 | | AttributeError Traceback (most recent call last) |
208 | | |
209 | | /Users/fangohr/local/hg/scico_web/debug/mysite/<ipython console> in <module>() |
210 | | |
211 | | /Library/Frameworks/Python.framework/Versions/4.0.30002/lib/python2.5/site-packages/django/contrib/auth/models.pyc in get_profile(self) |
212 | | 291 app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.') |
213 | | 292 model = models.get_model(app_label, model_name) |
214 | | --> 293 self._profile_cache = model._default_manager.get(user__id__exact=self.id) |
215 | | 294 except (ImportError, ImproperlyConfigured): |
216 | | 295 raise SiteProfileNotAvailable |
217 | | |
218 | | AttributeError: 'NoneType' object has no attribute '_default_manager' |
219 | | |
220 | | In [4]: |
221 | | }}} |
222 | | |
223 | | |
224 | | I have checked other entries on the mailing list, but couldn't find the solution to this problem. |
225 | | |
226 | | Any help is very welcome. |
227 | | |
228 | | Many thanks, |
229 | | |
230 | | Hans |
231 | | |
232 | | |
233 | | |
234 | | PS Version: |
235 | | {{{ |
236 | | phi:mysite fangohr$ python manage.py --version |
237 | | 1.0.2 final |
238 | | }}} |
239 | | PPS In the database for this example, the admin user is 'admin' and |
240 | | the password is 'admin' -- in case anybody wants to play with that. |
241 | | |
242 | | |
243 | | Solution (Karen Tracey): |
244 | | |
245 | | > (Thanks for the very detailed question.) I think you have found a |
246 | | > documentation bug. I do not believe the `'people'` part of that |
247 | | > should be normalized to lower case. It is not normalized to lower |
248 | | > case in `INSTALLED_APPS`, and I don't think it should be normalized |
249 | | > to lower case here. Since you have a capital P in People for your |
250 | | > directory name, try `'People.profile'`. |
251 | | > |
252 | | > |
253 | | > Karen |
254 | | |
255 | | Note: Changing `people.profile` to `People.profile` solves the problem. |
256 | | |
257 | | |
258 | | |
259 | | |
260 | | |
261 | | Related documentation: http://docs.djangoproject.com/en/dev/topics/auth/#auth-profiles |
262 | | |
263 | | Here it says under 1.: "The (normalized to lower-case) name of the application in which the user profile model is defined (in other words, an all-lowercase version of the name which was passed to `manage.py startapp` to create the application)." |
264 | | |
265 | | This is wrong. |
266 | | |
267 | | |