3

I have a django app called customer. Inside customer.models I have some model classes one of them is Tooth. I also created a new python file inside my app's directory called callbacks.py to store some callback functions for some signlas. What I do is the following

from customer.models import Tooth

def callback(sender, **kwargs)
    #using Tooth here

and on models.py

from customer.callbacks import callback
#.....
post_save.connect(callback, sender=Customer)

But when I try to run sqlall I get an import error

    from customer.models import Tooth
ImportError: cannot import name Tooth

Everything else all other imports work normally.

EDIT: Using Django 1.6 version

7
  • 1
    Does customer/models.py import callbacks.py? You might have a circular import here. Dec 16, 2014 at 13:37
  • Yes...but how can I use my callback on models.py file?
    – Apostolos
    Dec 16, 2014 at 13:51
  • There are various ways to do so, but the best way depends on the Django version you're using. Which version are you using? Do you mind upgrading if necessary? Dec 16, 2014 at 13:54
  • 1
    possible duplicate of the right place to keep my signals.py files in django. You'll also need to move your signal registration to the callbacks.py file (i.e. out of your models.py file, and remove the circular import). Dec 16, 2014 at 13:56
  • @ThomasOrozco but in django docs says that registering is better to be done in models.py: "Where should this code live? You can put signal handling and registration code anywhere you like. However, you’ll need to make sure that the module it’s in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app’s models.py a good place to put registration of signal handlers."
    – Apostolos
    Dec 16, 2014 at 14:02

1 Answer 1

1

This is a circular import.

Here's what happens:

  • Django loads up the models
  • Therefore, django imports customer.models
  • Python execute the content of customer/models.py to compute the attributes of the module
  • customers/models.py imports customer/callbacks.py
  • Python set aside the execution of customer/models.py and starts executing customer/callbacks.py
  • callbacks.py try to import models.py which is being imported. Python prevents double importation of the module and raises an ImportError.

Usually these kind of situation show a poor design. But sometimes (which seems to be your case) tight coupling is required. One quick and dirty way to fix this is to delay the circular import, in your case:

In models.py:

from customer.callbacks import callback

# Define Tooth

post_save.connect(callback, sender=Customer)

In callbacks.py:

def callback(sender, **kwargs)
    from customer.models import Tooth
    # Use Tooth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.