I read about solution for the error (write import instead of from ...) but it doesn't work I think because I have a complex folder structure.
quiz.models
import apps.courses.models as courses_models
courses_models.Lesson # some actions with that class
class Quiz:
pass
courses.models
import apps.quiz.models as quiz_models
quiz_models.Quiz # some actions with that class
class Lesson:
pass
Error:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/usr/local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/usr/local/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/code/apps/carts/models.py", line 5, in <module>
from ..courses.models import Course
File "/code/apps/courses/models.py", line 12, in <module>
import apps.quiz.models as quiz_models
File "/code/apps/quiz/models.py", line 12, in <module>
class Quiz(TimestampedModel):
File "/code/apps/quiz/models.py", line 15, in Quiz
courses_models.Lesson, default=1, on_delete=models.DO_NOTHING, verbose_name=_('Lesson'), related_name='quizzes')
AttributeError: partially initialized module 'apps.courses.models' has no attribute 'Lesson' (most likely due to a circular import)
Maybe you have other solutions for circular importing or I just wrote the imports incorrectly?
Yes, this is a circular import. course.models is importing quiz.models while quiz.models is also importing course.models.
To avoid this, you could defer the import until you really need it.
Related
I have split the Django models into multiple model files following the follow file tree structure,
+-api(app)-+
+-__init__.py
+-models -+
|
+-__init__.py
+-model1.py
+-model2.py
+-model3.py
+-serializers-+
|
+-__init__.py
+- model1_serializer.py
+-views
+-apps.py
...
my __init__.py in models looks like,
from .model1 import *
from .model2 import *
and serializer __init__.py files look like this,
from .model1_serializer import MBTITypeSerializer
I have splitter views files and serializer files. When I try to import models some of them imports without any problem, but some imports not working. I have observed if I change the import order in __init__.py file the working imports change. This is how I tried to import models,
in serializers
from api.models import MBTIType
...
Here is the error trace,
Traceback (most recent call last):
File "C:\Users\ \AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\ \AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "D:\ \implementation\backend\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\\implementation\backend\venv\lib\site-packages\django\core\management\commands\runserver.py", line 110, in inner_run
autoreload.raise_last_exception()
File "D:\\implementation\backend\venv\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise _exception[1]
File "D:\\implementation\backend\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
autoreload.check_errors(django.setup)()
File "D:\\implementation\backend\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\\implementation\backend\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\\implementation\backend\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "D:\\implementation\backend\venv\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\\implementation\backend\api\models\__init__.py", line 2, in <module>
from .model1 import *
File "D:\\implementation\backend\api\models\model1.py", line 3, in <module>
from .model2 import Model2
File "D:\\implementation\backend\api\models\model2.py", line 5, in <module>
from api.serializers import serilizer1
File "D:\\implementation\backend\api\serializers\__init__.py", line 2, in <module>
from .model1_serializer import Model1Serializer
File "D:\\implementation\backend\api\serializers\model1_serializer.py", line 2, in <module>
from api.models import Model1
ImportError: cannot import name 'Model1' from 'api.models' (D:\\implementation\backend\api\models\__init__.py)
Hoping any guidance to solve the issue.
check your model2.py, you have there on line 5 from api.serializers import serilizer1
But you don't need to have serializer1 in models.
I have just started learning Django frame work and got stuck after getting an unexpected error as below:-(I have tried to post all information, which i find necessary to be here)
TypeError: __init__() got an unexpected keyword argument 'upload_to'
I got this error while running command python manage.py makemigrations
My project contains only one Model class Products
My Model Class:-
from django.db import models
# Create your models here.
class Products(models.Model):
name = models.CharField(max_length=25)
price = models.IntegerField()
description = models.CharField(max_length=50)
pic = models.DateTimeField(upload_to="myimages")
I am added my app in INSTALLED_APPS fields in setting.py
Full Error Track:-
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\ecommerceproject\manage.py", line 22, in <module>
main()
File "C:\Users\Admin\PycharmProjects\ecommerceproject\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\Admin\PycharmProjects\ecommerceproject\products\models.py", line 5, in <module>
class Products(models.Model):
File "C:\Users\Admin\PycharmProjects\ecommerceproject\products\models.py", line 9, in Products
pic = models.DateTimeField(upload_to="myimages")
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1158, in __init__
super().__init__(verbose_name, name, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'upload_to'
C:\Users\Admin\PycharmProjects\ecommerceproject>cls
C:\Users\Admin\PycharmProjects\ecommerceproject>python manage.py makemigrations
Traceback (most recent call last):
File "C:\Users\Admin\PycharmProjects\ecommerceproject\manage.py", line 22, in <module>
main()
File "C:\Users\Admin\PycharmProjects\ecommerceproject\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\Admin\PycharmProjects\ecommerceproject\products\models.py", line 5, in <module>
class Products(models.Model):
File "C:\Users\Admin\PycharmProjects\ecommerceproject\products\models.py", line 9, in Products
pic = models.DateTimeField(upload_to="myimages")
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1158, in __init__
super().__init__(verbose_name, name, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'upload_to'
Thanks in advance.
Hope to here from you soon.
It doesn't seem like DateTimeField accepts the upload_to keyword argument.
pic = models.DateTimeField(upload_to="myimages")
See docs.
Were you trying to use another Model Field? e.g., ImageField, FileField
You are getting error because of it. Try with proper field
pic = models.DateTimeField(upload_to="myimages")
I'm trying to make a model that needs a relationship of one to one with a feedstock that is used by a plenty of formulas. This is my code:
from django.db import models
from dashboard.models import Formulas, Feedstock
class FeedstockFormulas(models.Model):
ratio = models.FloatField()
feedstock = models.OneToOneField(Feedstock, on_delete=models.CASCADE, default="0")
formulas = models.ForeignKey(Formulas, on_delete=models.CASCADE)
This is the error I'm getting:
Traceback (most recent call last):
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 798, in __init__
to._meta.model_name
AttributeError: module 'dashboard.models.Feedstock' has no attribute '_meta'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/__init__.py", line 1, in <module>
from .Formulas import Formulas
File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/Formulas.py", line 2, in <module>
from dashboard.models import FeedstockFormulas
File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/FeedstockFormulas.py", line 4, in <module>
class FeedstockFormulas(models.Model):
File "/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/FeedstockFormulas.py", line 6, in FeedstockFormulas
feedstock = models.OneToOneField(Feedstock, on_delete=models.CASCADE, default="0")
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 1049, in __init__
super().__init__(to, on_delete, to_field=to_field, **kwargs)
File "/home/isaacrpl7/.local/lib/python3.8/site-packages/django/db/models/fields/related.py", line 800, in __init__
assert isinstance(to, str), (
AssertionError: OneToOneField(<module 'dashboard.models.Feedstock' from '/mnt/c/Users/isaac/Documents/Github/uniaoracoes/dashboard/models/Feedstock.py'>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'
I can't really solve it, since the first parameter to ForeignKey is already a model...
It looks like this is a by-product of a circular import somewhere.
Try changing to
feedstock = models.OneToOneField("dashboard.Feedstock", on_delete=models.CASCADE, default="0")
formulas = models.ForeignKey("dashboard.Formulas", on_delete=models.CASCADE)
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x105de5048>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/Name/Desktop/ProjectDevCode/Restructured/mysite/seasons/models.py", line 2, in <module>
from users.models import User
File "/Users/Name/Desktop/ProjectDevCode/Restructured/mysite/users/models.py", line 11, in <module>
from seasons.models import Seasons
ImportError: cannot import name 'Seasons'
I randomly got this error in my recent django app. I am not sure why. The models import in other apps/files in my project. Any help is appreciated.
I believe you faced circular model import. Seems like you are importing User into seasons.model and Seasons into users.model.
If you use them as a foreign key you can set it as:
user = models.ForeignKey('users.User')
So I'm trying to add some code to my Django 1.10 app that will only run when the server starts. I took the instructions on how to do this from here and here. Every time I try running the python console I get this:
C:\Users\username\Envs\projectName [master ≡ +0 ~2 -0 !]> python manage.py shell
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 341, in execute
django.setup()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Users\username\AppData\Local\Programs\Python\Python3\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in_call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in_call_with_frames_removed
File "C:\Users\username\Envs\projectName\appName\apps.py", line 2, in <module>
import appName.osm as osm
File "C:\Users\username\Envs\projectName\appName\osm.py", line 4, in <module>
from .models import Country, Embassy
File "C:\Users\username\Envs\projectName\appName\models.py", line 38, in <module>
class Country(models.Model):
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\apps\registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I tried looking this up but it seems that everyone who gets this exception is because it relates to an update issue with Django, however I've been using 1.10 for a long time now (not sure if I ever touched anything older). Any idea what's going on?
projectName/appName/apps.py:
from django.apps import AppConfig
import finder.osm as osm # this seems to cause the issue
class FinderConfig(AppConfig):
name = 'finder'
init = True
def ready(self):
pass
projectName/appName/__init__.py:
default_app_config = 'appName.apps.AppNameConfig'
projectName/appName/models.py:
from django.db import models
class Country(models.Model):
#...
class Embassy(models.Model):
#...
As #Andrey Shipilov has commented, it was a case of bad imports as you cannot import on the same level as the AppConfig class. A fix to my solution was to move the import into AppConfig.ready()
projectName/appName/apps.py:
from django.apps import AppConfig
class FinderConfig(AppConfig):
import finder.osm as osm
name = 'finder'
init = True
def ready(self):
pass