encountering errors while adding a foreign key in models.py - python

I wanted to migrate some models but the ones with the foreignkey functions are causing some problems.
I ran the following code and it caused an error
python manage.py migrate
and the foreign key seems the problem
class Webpage(models.Model):
topic =models.ForeignKey(Topic)
name = models.CharField(max_length=264 , unique=True)
url = models.URLField(unique=True)
def __str__(self):
return self.name
Traceback (most recent call last):
File "C:\Users\helin\Anaconda3\envs\myenv\lib\sitepackages\django\utils\autoreload.py", line 225, in wrapper fn(*args, **kwargs)
File "C:\Users\helin\Anaconda3\envs\myenv\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\helin\Anaconda3\envs\myenv\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "C:\Users\helin\Anaconda3\envs\myenv\lib\site-packages\django\core\management\__init__.py", line 337, in execute autoreload.check_errors(django.setup)()
File "C:\Users\helin\Anaconda3\envs\myenv\lib\sitepackages\django\utils\autoreload.py", line 225, in wrapper fn(*args, **kwargs)
File "C:\Users\helin\Anaconda3\envs\myenv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\helin\Anaconda3\envs\myenv\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\helin\Anaconda3\envs\myenv\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\helin\Anaconda3\envs\myenv\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 985, in _gcd_import
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 697, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\Users\helin\Downloads\my_django\first_project\first_app\models.py", line 12, in <module>
class Webpage(models.Model):
File "C:\Users\helin\Downloads\my_django\first_project\first_app\models.py", line 13, in Webpage
topic =models.ForeignKey(Topic)
TypeError: __init__() missing 1 required positional argument: 'on_delete'

Yes, you are missing 1 required positional argument: 'on_delete'.
When using foreign key, you need to pass 'on_delete' argument to it. This will make sure that whenever the table/ record to which this foreign key is a primary key is deleted it will automatically delete/protect the record of the table where it acts as a foreign key.
Example:
class Customer(models.Model):
customer_id = <something>
class Account(models.Model):
customer_id = models.ForeignKey(Customer, on_delete=models.CASCADE)
So, when a customer with customer_id = x is deleted in Customer related table, it will delete the corresponding record in the Account table.
You can read about it here

Related

Getting TypeError: __init__() got an unexpected keyword argument 'upload_to' in Django Project?

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")

Django migration fails 'QuerySet' object has no attribute 'objects'

I have a set of models:
class DebugConf(models.Model):
is_setup = models.BooleanField(default=False)
debug_setup_date = models.DateTimeField()
def __str__(self):
return self.is_setup
class Currency(models.Model):
currency_name = models.CharField(max_length=100)
currency_value_in_dollars = models.FloatField()
currency_value_in_dollars_date = models.DateTimeField()
def __str__(self):
return self.currency_name
class User(models.Model):
user_name = models.CharField(max_length=200)
user_pass = models.CharField(max_length=200)
join_date = models.DateTimeField()
def __str__(self):
return self.user_name
class Transaction(models.Model):
transaction_type = models.CharField(max_length=200)
transaction_amount = models.FloatField()
transaction_date = models.DateTimeField()
transaction_currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
transaction_users = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.headline
a project 'crypto' and an app 'manage_crypto_currency' nested into it:
The app's views contains some initialization code:
views.py:
if IS_DEBUG_MODE:
print('[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES')
init_debug_tables()
def init_debug_tables():
# Check if debug table has already been initialized
debug_conf = DebugConf.objects.all()
if debug_conf.objects.exists():
return
At the moment, the db is not initialized; when running:
python manage.py makemigrations manage_crypto_currency
in the root (project dir):
I get:
[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES
Traceback (most recent call last):
File "manage.py", line 24, in <module>
main()
File "manage.py", line 20, in main
execute_from_command_line(sys.argv)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 368, in execute
self.check()
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\msebi\AppData\Local\Programs\Python\Python38-32\lib\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 "C:\projects\django\crypto-currency-board\crypto\crypto\urls.py", line 21, in <module>
path('', include('manage_crypto_currency.urls')),
File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\msebi\AppData\Local\Programs\Python\Python38-32\lib\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 "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\urls.py", line 5, in <module>
from . import views
File "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\views.py", line 24, in <module>
init_debug_tables()
File "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\setup_test_db\setup_debug_tables.py", line 14, in init_debug_tables
if debug_conf.objects.exists():
AttributeError: 'QuerySet' object has no attribute 'objects'
The line:
[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES
is in views. Apparently, the migrations tool feels the need to execute views.py. Why on Earth, I have no clue. Is it possible to make makemigrations NOT execute code that it shouldn't, e.g. just create the models given that the db is not initialized?
EDIT
The answers don't answer WHY migrate runs views.py (I don't know either), but commenting out:
# if IS_DEBUG_MODE:
# print('[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES')
# init_debug_tables()
leads to successful migration. Is there a less cumbersome way to go about it? (Un)comment is ugly.
You don't need to use call the objects of a QuerySet
def init_debug_tables():
# Check if debug table has already been initialized
debug_conf = DebugConf.objects.all()
if debug_conf.exists(): # instead of `debug_conf.objects.exists()`
return
As the error message suggest, debug_conf is the the model class, it is an instance of a QuerySet.
objects is the Manager attached to a model class. This managers creates QuerySet for you (in your code, by the return value of the all method).
So you want to do this:
def init_debug_tables():
# Check if debug table has already been initialized
debug_conf = DebugConf.objects.all()
if debug_conf.exists():
return

How to solve this django OneToOneField model error?

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)

Error using Textfield and charfield model in Django

I have been trying to use Textfield in class but the suggestions just does not show up. Instead i receive an error in my Cmd console. This was even the case while using CharField.
Below is my code:
from django.db import models
from django.db.models import CharField
from django.db.models import Textfield
from datetime import datetime
class Posts(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
created_at = models.DateTimeField(default=datetime.now, blank=True)
This is the error i get in my CMD:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "d:\python\python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "d:\python\python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\core\management\commands\runserver.py", line
109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 76, in
raise_last_exception
raise _exception[1]
File "C:\Users\USER\Envs\py1\lib\site-packages\django\core\management\__init__.py", line 357, in
execute
autoreload.check_errors(django.setup)()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "d:\python\python38\lib\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 "E:\Python\djangoproject\posts\models.py", line 3, in <module>
from django.db.models import Textfield
ImportError: cannot import name 'Textfield' from 'django.db.models' (C:\Users\USER\Envs\py1\lib\site-
packages\django\db\models\__init__.py)
Actually you don't need to import the fields separately, you could use the django.db.models that you have imported earlier.
And the error is produced because you should use TextField with a capital F

Django circular import AppRegistryNotReady error

i' ve Django 1.9.2 with python 3.4.2 in a virtualenvironment.
I' ve many applications, and the 2 related are common and shop.
common/models.py contains:
from django.apps import apps
class Document(CLDate):
user = models.ForeignKey(User)
assessmentorder = models.ForeignKey(apps.get_model('shop', 'AssessmentOrder'), blank=True, null=True)
shop/models.py contains:
from common.models import ServiceModel
class AssessmentOrder(CLDate):
"""AssessmentOrder model"""
order = models.ForeignKey(Order)
comment = models.TextField()
.
This is a circular import, and i read many strategy to resolve it (including apps.get_model), but none of them seem to work for me. I also tried
apps.get_model('shop.AssessmentOrder')
, but the same. The complete error message is the following:
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/hidden/git/sccdb/sccdb/common/models.py", line 25, in <module>
class Document(CLDate):
File "/home/hidden/git/sccdb/sccdb/common/models.py", line 28, in Document
assessmentorder = models.ForeignKey(apps.get_model('shop.AssessmentOrder'), blank=True, null=True)
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/apps/registry.py", line 194, in get_model
self.check_models_ready()
File "/home/hidden/.virtualenvs/sccdb34/lib/python3.4/site-packages/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Is it somehow related to my django version or python3, or what am i doing wrong?
Instead of doing using get_model function in foreign key declaration, you can simply put the model name as string and it'll still work:
assessmentorder = models.ForeignKey('shop.AssessmentOrder', blank=True, null=True)
This should resolve the issue
Don't include models, just put a path to them. As it described in docs
from django.conf import settings
class Document(CLDate):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
assessmentorder = models.ForeignKey('shop.AssessmentOrder', blank=True, null=True)
And
class AssessmentOrder(CLDate):
"""AssessmentOrder model"""
order = models.ForeignKey('yourapp.Order')
comment = models.TextField()

Categories