views:

497

answers:

1

I'm developing a django application. Modules of importance to my problem are given below:

globals.py --> contains constants that are used throughout the application. SITE_NAME and SITE_DOMAIN are two of those and are used to fill some strings. Here is how I define them:

from django.contrib.sites.models import Site
...
SITE_DOMAIN = Site.objects.get_current().domain
SITE_NAME = Site.objects.get_current().name

models.py --> models live inside this module. imports some constants from globals.py

some_command.py --> a command that imports some constants from globals also.

when executed, the command imports a constant from globals.py and runs into a circular import problem: inside globals.py, get_current() from sites app is called, and sites app in turn imports models.py which has imports from globals.py as well.

EDIT:

The application runs flawlessly, without encountering this circular import issue. Importing globals.py from shell brings no problems. Even the command can be executed from the shell without calling manage.py.

So why does manage.py some_command fail due to a circular import?

Thanks in advance.

+1  A: 

Is there any particular reason you need to store SITE_DOMAIN and SITE_NAME in globals.py? These are already available directly from the sites framework.

According to the docs, the site object is cached the first time you access it, so importing it and using it there directly doesn't hurt.

R. Bemrose
Some string constants in the globals.py use them, such as this imaginary one: WELCOME_STRING = 'Welcome to' % SITE_NAME
shanyu
Lol, welcome string constant. Do you also write some of your views in globals.py? http://www.c2.com/cgi/wiki?GlobalVariablesAreBad
drozzy
@drozzy "globals.py" is just a container for strings that are translated. It is no more "global" than, say, settings.py. Do not prejudge by its name ;)
shanyu