Django signals file, cannot import model names

怎甘沉沦 提交于 2020-01-02 05:24:11

问题


I have such file order:

project/
    app/
        models.py
        signals.py

I am keeping signals inside signals.py as it should be. and at the top of the signals.py file, I include myapp models as I do queries in these signals with

from myproject.myapp.models import Foo

However it doesnt seem to find it, as I run the server or validate from manage.py, it gives this error:

   from myproject.myapp.models import Foo
ImportError: cannot import name Foo

I am using Django 1.2.1.


回答1:


Most likely you have a circular dependency. Does your models.py import the signals? If so, this can't work as both modules now depend on each other. You may need to import the models within a function in the signals file, rather than at the top level.



来源:https://stackoverflow.com/questions/3108694/django-signals-file-cannot-import-model-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!