Signals problem

36 views
Skip to first unread message

Joel Klabo

unread,
Sep 23, 2010, 2:41:06 AM9/23/10
to Django users
I keep getting this import error and I can't figure out why? Any ideas
would be greatly appreciated: http://dpaste.org/BgtI/

Joel Klabo

unread,
Sep 23, 2010, 2:50:58 AM9/23/10
to Django users

Russell Keith-Magee

unread,
Sep 23, 2010, 2:52:13 AM9/23/10
to django...@googlegroups.com
On Thu, Sep 23, 2010 at 2:41 PM, Joel Klabo <joel...@gmail.com> wrote:
> I keep getting this import error and I can't figure out why? Any ideas
> would be greatly appreciated: http://dpaste.org/BgtI/

It's a circular import problem. signals.py imports objects from
models.py, and models.py imports objects from signals.py. Python can't
handle circular imports -- hence, the import error.

To fix this, you either need to:

* Put everything in models.py
* Refactor signals.py so that it doesn't need to import models.py at
the global level.

The second approach can be handled in two ways -- either make the
import internal to the 'drink_activity' method:

def drink_activity(sender, **kwargs):
from models import Activity
....

or use Django's dynamic model loading to determine the model at runtime:

from django.db.models import get_model

def drink_activity(sender, **kwargs):
Activity = get_model('myapp','Activity)
...

Django 1.3 will probably introduce a third option -- a reliable place
to register signals. We need this for completely separate reasons, but
providing a safe home for signal registration will be a happy
consequence.

Yours,
Russ Magee %-)

Joel Klabo

unread,
Sep 23, 2010, 3:04:57 AM9/23/10
to Django users
Thanks for the help, all working now!

On Sep 22, 11:52 pm, Russell Keith-Magee <russ...@keith-magee.com>
wrote:
Reply all
Reply to author
Forward
0 new messages