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 imports customer/callbacks.py
  • python set aside execution of customer/models.py , starts executing customer/callbacks.py
  • callbacks.py try import models.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

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -