12👍
You are doing what is known as a Circular import.
models.py:
from bm.bmApp.utils import to_safe_uppercase
utils.py:
from bm.bmApp.models import Client
Now when you do import bm.bmApp.models
The interpreter does the following:
models.py - Line 1
: try to importbm.bmApp.utils
utils.py - Line 1
: try to importbm.bmApp.models
models.py - Line 1
: try to importbm.bmApp.utils
utils.py - Line 1
: try to importbm.bmApp.models
- …
The easiest solution is to move the import inside the function:
utils.py:
def get_client(user):
from bm.bmApp.models import Client
try:
client = Client.objects.get(username=user.username)
except Client.DoesNotExist:
print "User Does not Exist"
return None
else:
return client
def to_safe_uppercase(string):
if string is None:
return ''
return string.upper()
8👍
You are creating a circular import.
utils.py
from bm.bmApp.models import Client
# Rest of the file...
models.py
from bm.bmApp.utils import to_safe_uppercase
# Rest of the file...
I would suggest your refactor your code so that you don’t have a circular dependency (i.e. utils should not need to import models.py or vice versa).
- Query a template for the variables it needs?
- Closing db connection with django's persistent connection in a multi-threaded script
- Django, Cannot assign None, does not allow null values
- How to redirect users to a specific url after registration in django registration?
0👍
I’m not sure I can explain the Import error, but I have three ideas. First, your function needs tweaking. You’ve used a reserved word ‘string’ as an argument. Consider renaming.
Second, what happens if you invoke ./manage.py shell, and do the import by hand. Does it give you any different?
Third, try deleting your pyc files to force django to recompile python code (this one is a very long shot…but worth eliminating)
- How to fix Error: pg_config executable not found on Elastic Beanstalk permanently
- Add timestamp and username to log