python - Error importing class in django -
i have django app called customer. inside customer.models
have model classes 1 of them tooth. created new python file inside app's directory called callbacks.py store callback functions signlas. 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 try run sqlall import error
customer.models import tooth importerror: cannot import name tooth
everything else other imports work normally.
edit: using django 1.6 version
this circular import.
here's happens:
- django loads models
- therefore, django imports
customer.models
- python execute content of
customer/models.py
compute attributes of module customers/models.py
importscustomer/callbacks.py
- python set aside execution of
customer/models.py
, starts executingcustomer/callbacks.py
callbacks.py
try importmodels.py
being imported. python prevents double importation of module , raises importerror.
usually these kind of situation show poor design. (which seems case) tight coupling required. 1 quick , dirty way fix delay circular import, in case:
in models.py
:
from customer.callbacks import callback # define tooth post_save.connect(callback, sender=customer)
in callbacks.py
:
def callback(sender, **kwargs) customer.models import tooth # use tooth
Comments
Post a Comment