Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - circular import problem when executing a command

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.

like image 394
shanyu Avatar asked Jul 05 '26 07:07

shanyu


1 Answers

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.

like image 85
Powerlord Avatar answered Jul 06 '26 21:07

Powerlord