0

In my Django app (lets call it app) I have a number of files: views.py, models.py and I created my own utils.py.

Unfortunately, while I can include my models in my views.py file simply by saying

from models import *

In my utils.py file, if I try the same thing, and then work with a model, I get an exception Global name: MyModel is not defined. models.py does indeed include utils.py, and I understand this may be a circular dependcy, but it worked fine until I added a recent change. Is this the cause, if so is the only solution to refactor my utils file?

1 Answer 1

1

Well you already know it is because of mutual dependencies. The way around it would be to split the util file in to two so, that you could avoid circular imports by separating the parts where you are required to call the models.

Also, as suggested by Mipadi instead of using a global import statement you could simply make the import in the method scope

Moreover, it would really depend how you are trying to use the models. For instance, you could access the models by "app_name.class_name" but really depends on the context in which you want to use.

Sign up to request clarification or add additional context in comments.

2 Comments

Or utils could import models at a narrower scope (such as just in a function) to avoid circular dependency issues.
Oh yes..forgot to mention that

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.