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
This is a circular import.
Here's what happens:
customer.modelscustomer/models.py to compute the attributes of the modulecustomers/models.py imports customer/callbacks.pycustomer/models.py and starts executing customer/callbacks.pycallbacks.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With