On 6 Giu, 16:31, Karen Tracey <kmtra...@gmail.com> wrote: > On Sun, Jun 6, 2010 at 9:14 AM, drakkan <drakkan1...@gmail.com> wrote: > > I'm migrating my sites to django 1.2.1 however I noticed my cron > > scripts doesn't work anymore with the latest django version, I'm using > > the setup_environ method > > >http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/ > > > and I tryed also the DJANGO_SETTINGS_MODULE environment variable > > method with the same results: > > > 1) my cron script work fine in 1.1.1 but not in 1.1.2 and 1.2.1 > > 2) the script exit with a trace such this: > > > Traceback (most recent call last): > > File "syncdomains.py", line 16, in <module> > > from systemcp.domini.models import Domain, ProtectedArea, > > HttpErrors > > File "/home/nicola/workspace/SystemPanel/src/systemcp/domini/ > > models.py", line 3, in <module> > > from systemcp.utenti.models import UserInfo > > File "/home/nicola/workspace/SystemPanel/src/systemcp/utenti/ > > models.py", line 76, in <module> > > admin.site.register(UserInfo,UserInfoAdmin) > > File "/usr/lib/python2.6/site-packages/django/contrib/admin/ > > sites.py", line 90, in register > > validate(admin_class, model) > > File "/usr/lib/python2.6/site-packages/django/contrib/admin/ > > validation.py", line 20, in validate > > models.get_apps() > > File "/usr/lib/python2.6/site-packages/django/db/models/loading.py", > > line 115, in get_apps > > self._populate() > > File "/usr/lib/python2.6/site-packages/django/db/models/loading.py", > > line 64, in _populate > > self.load_app(app_name) > > File "/usr/lib/python2.6/site-packages/django/db/models/loading.py", > > line 78, in load_app > > models = import_module('.models', app_name) > > File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", > > line 35, in import_module > > __import__(name) > > File "/home/nicola/workspace/SystemPanel/src/systemcp/ftp/ > > models.py", line 4, in <module> > > from systemcp.domini.models import Domain > > ImportError: cannot import name Domain > > > this trace tipycally mean circolar import but I'm sure I don't have > > this error in my code, maybe I have to change my envirornment > > initialization for cron script since 1.1.2? > > > If I execute the same imports I do in my cron script using python > > manage.py shell, they works fine so I think something changed in the > > environment initialization > > > any hints? > > There is a circular import. The code is in the process of importing Domain > and hits code where it again attempts to import Domain. The resulting > ImportError was mistakenly suppressed in 1.1, this issue was reported in > ticket #11696 (http://code.djangoproject.com/ticket/11696) and the fix for > that has caused the problem to surface in your cron script. > > Now, the circular import is not directly in your app code. What's happening > is your app code, in models.py, is registering models with the admin (line > 76 of /home/nicola/workspace/ > SystemPanel/src/systemcp/utenti/models.py is attempting to register an admin > module for UserInfo.When DEBUG is on, admin registrations trigger validation > code that first loads all app models, and that is what is resulting in the > circular import, because your cron script is already in the process of > importing one of the models. > > One fix would be to remove the admin registrations from models.py: that's > generally not a good place for them. Having them in models.py tends to lead > to mysteriously missing models in the admin when you run on production > server with debug off, since then models.py is not necessarily loaded before > the admin code is called to process requests. Moving admin registrations to > an admin.py file and calling admin.autodiscover() from urls.py is the > currently best-practice way of ensuring admin registrations are done when > they need to be...and it would avoid this circular import you are seeing in > your cron script. > > Another way to fix it would be to turn of DEBUG when running the cron > script. Then the admin validation code won't be called, which will avoid the > circular import. But really, admin registrations should not be done in > models.py. > > Karen > --http://tracey.org/kmt/
Thanks Karen, I moved all the admin registrations in a separate admin.py file and now all is fine, drakkan -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.