I created a vanilla project using 'django-admin.py
startproject
mysite' and then created a vanilla app in mysite using
'manage.py
startapp myapp'. I ensured that 'mysite.myapp' was added to
mysite.settings.INSTALLED_APPS, and also updated some other
settings
in mysite.settings.py. All well and good.
Now, myapp is the package for my application, and so in its
__init__.py, I want to do some setting up. I wanted some
information
from the User model in django.contrib.auth.models, and so I
imported
django.contrib.auth.models - this led to an error, though I
was able
to import django.contrib.auth without any problems.
With the following myapp/__init__.py:
import django.contrib.auth
print django.contrib.auth
#import django.contrib.auth.models
When I ran 'manage.py diffsettings', I got the following
output, as
expected
(please excuse line-wrap if it happens):
vinay zeta-dapper:~/projects/mysite$ ./manage.py
diffsettings
<module 'django.contrib.auth' from
'/usr/lib/python2.4/site-packages/django/contrib/auth/__init
__.pyc'>
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/home/vinay/projects/mysite.db'
DEBUG = True
INSTALLED_APPS = ['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sites',
'mysite.myapp']
LANGUAGE_CODE = 'en-gb'
MIDDLEWARE_CLASSES =
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware')
ROOT_URLCONF = 'mysite.urls' ###
SECRET_KEY =
'u_wf!1^!3y7iz!)=3cs8-=clsg^#g&2y16cavha2!fca3macrw'
SETTINGS_MODULE = 'mysite.settings' ###
SITE_ID = 1 ###
TEMPLATE_DEBUG = True
TIME_ZONE = 'Europe/bond'
However, uncommenting the last line in myapp/__init__.py and
running
'manage.py
diffsettings' led to an error:
vinay zeta-dapper:~/projects/mysite$ ./manage.py
diffsettings
<module 'django.contrib.auth' from
'/usr/lib/python2.4/site-packages/django/contrib/auth/__init
__.pyc'>
Traceback (most recent call last):
File "./manage.py", line 11, in ?
execute_manager(settings)
File
"/usr/lib/python2.4/site-packages/django/core/managemen
t.py",
line 1680, in execute_manager
execute_from_command_line(action_mapping, argv)
File
"/usr/lib/python2.4/site-packages/django/core/managemen
t.py",
line 1572, in execute_from_command_line
translation.activate('en-us')
File
"/usr/lib/python2.4/site-packages/django/utils/translat
ion/
trans_real.py", line 195, in activate
_active[currentThread()] = translation(language)
File
"/usr/lib/python2.4/site-packages/django/utils/translat
ion/
trans_real.py", line 184, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File
"/usr/lib/python2.4/site-packages/django/utils/translat
ion/
trans_real.py", line
167, in _fetch
app = getattr(__import__(appname[:p], {}, {},
[appname[p+1:]]),
appname[p+1:])
File
"/home/vinay/projects/mysite/../mysite/myapp/__init__.p
y", line
3, in ?
import django.contrib.auth.models
File
"/usr/lib/python2.4/site-packages/django/contrib/auth/
models.py", line 4,
in ?
from django.contrib.contenttypes.models import
ContentType
File
"/usr/lib/python2.4/site-packages/django/contrib/conten
ttypes/
models.py",
line 33, in ?
class ContentType(models.Model):
File
"/usr/lib/python2.4/site-packages/django/db/models/base
.py",
line 30, in
__new__
new_class.add_to_class('_meta',
Options(attrs.pop('Meta', None)))
File
"/usr/lib/python2.4/site-packages/django/db/models/base
.py",
line 169, in
add_to_class
value.contribute_to_class(cls, name)
File
"/usr/lib/python2.4/site-packages/django/db/models/opti
ons.py",
line 53,
in contribute_to_class
setattr(self, 'verbose_name_plural',
meta_attrs.pop('verbose_name_plural',
self.verbose_name + 's'))
File
"/usr/lib/python2.4/site-packages/django/utils/function
al.py",
line 42,
in __wrapper__
res = self.__func(*self.__args, **self.__kw)
File
"/usr/lib/python2.4/site-packages/django/utils/translat
ion/
trans_real.py", line
255, in gettext
_default = translation(settings.LANGUAGE_CODE)
File
"/usr/lib/python2.4/site-packages/django/utils/translat
ion/
trans_real.py", line
184, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File
"/usr/lib/python2.4/site-packages/django/utils/translat
ion/
trans_real.py", line
167, in _fetch
app = getattr(__import__(appname[:p], {}, {},
[appname[p+1:]]),
appname[p+1:])
AttributeError: 'module' object has no attribute 'myapp'
Now, why should this be? The traceback is mystifying to me -
can
anyone shed any
light? I certainly want, in myapp's setup, to do some things
based on
django.contrib.auth.models.User. (For example, I might want
to set the
email
field to be unique, which it is not by default). It seemed
reasonable
to assume
that all the apps would be initialised in the order that
they appear
in
INSTALLED_APPS, and mysite.myapp (being last in the list)
should be
able to rely
on the preceding apps being initialised already. If this is
not the
case, where
is the best place to do the kind of initialisation I want
to?
Thanks in advance,
Vinay Sajip
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|