[Django rest_framework]Problems in using redis as Django DRF cache - python

I'm trying to open the redis cache for the rest framework.
My configuration seems to be correct.
When I run, it reports an error. As shown below:
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\settings.py", line 177, in import_from_string
return import_string(val)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
module = import_module(module_path)
File "C:\Users\HUANG\AppData\Local\Programs\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 "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework_extensions\utils.py", line 6, in <module>
from rest_framework_extensions.key_constructor.constructors import (
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework_extensions\key_constructor\constructors.py", line 4, in <module>
from rest_framework_extensions.key_constructor import bits
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework_extensions\key_constructor\bits.py", line 3, in <module>
from django.db.models.sql.datastructures import EmptyResultSet
ImportError: cannot import name 'EmptyResultSet' from 'django.db.models.sql.datastructures' (C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\datastructures.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\HUANG\AppData\Local\Programs\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 "D:\C_项目相关\PythonWeb\TakeMeHome\TakeMeHome\urls.py", line 20, in <module>
from Website import views as views2
File "D:\C_项目相关\PythonWeb\TakeMeHome\Website\views.py", line 155, in <module>
class Cache_test(APIView,):
File "D:\C_项目相关\PythonWeb\TakeMeHome\Website\views.py", line 158, in Cache_test
#cache_response(timeout=60)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework_extensions\cache\decorators.py", line 38, in __init__
self.key_func = extensions_api_settings.DEFAULT_CACHE_KEY_FUNC
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\settings.py", line 220, in __getattr__
val = perform_import(val, attr)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\settings.py", line 166, in perform_import
return import_from_string(val, setting_name)
File "C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\settings.py", line 180, in import_from_string
raise ImportError(msg)
ImportError: Could not import 'rest_framework_extensions.utils.default_cache_key_func' for API setting 'DEFAULT_CACHE_KEY_FUNC'. ImportError: cannot import name 'EmptyResultSet' from 'django.db.models.sql.datastructures' (C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\datastructures.py).
**ImportError: Could not import 'rest_framework_extensions.utils.default_cache_key_func' for API setting 'DEFAULT_CACHE_KEY_FUNC'.
ImportError: cannot import name 'EmptyResultSet' from 'django.db.models.sql.datastructures' (C:\Users\HUANG\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\datastructures.py).**
It's frustrating that I'm a beginner and a rookie, I can't fix it.
And I didn't find the answer on the Internet, I can only try to come here and ask for your help
version:
Django: 3.2.10
drf : 3.11
redis: 3.0.2 (windows release)
my settings:
CACHES = {
'default':{
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/0',## if redis server has passwd,
# "LOCATION": "password#redis://127. 0.0.1:6379",
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
}
},
}
#
REST_FRAMEWORK_EXTENSIONS = {
'DEFAULT_CACHE_KEY_FUNC':
'rest_framework_extensions.utils.default_cache_key_func'
}
#views.py Like this:
from Users.serializers import UserAddrModelSerializer
from rest_framework_extensions.cache.decorators import cache_response
class Cache_test(APIView,):
permission_classes = []
throttle_classes = (AnonRateThrottle, UserRateThrottle)
#cache_response(timeout=60)
def get(self, request, *args, **kwargs):
obj = UserAddress.objects.all()
result = UserAddrModelSerializer(instance=obj, many=True).data
return Response(result)

This particular error is caused by drf-extensions not being compatible yet with Django 3.1+. (drf-extensions is one of the dependencies of your app; also, you say you're using 3.2.10, but 3.2 is still under development; the latest released version as of writing is 3.1.1) I've found a commit to the drf-extensions project on GitHub solving this issue, but that has not found its way yet to a release.
Some of your options:
Check if you really need the dependency, and if not, uninstall it. Depending on your caching needs, simply using Django's built-in caching framework might suffice.
Incorporate the fix in that commit that I linked into your installation of that dependency (i.e. patch your library locally).
Downgrade your Django installation in the meantime to version 3.0 (as a last resort).

Related

Getting mistake after creating success_url with CreateView class

Sorry for my bad English in advance.
I'm trying to discover django, so I decided to write my own sign in/sign up views.
So I just wrote something like that -
class RegisterView(CreateView):
template_name = 'auth/user_create_form.html'
model = User
fields = ['username', 'password', 'email']
So, it's basically working with GET requests. We can see the form on specified url.
But after altering all fields and submitting form, I've got a mistake what suggested to specify get_absolute_url for model to redirect after submitting. So I decided to give success_url to RegisterView -
class RegisterView(CreateView):
template_name = 'auth/user_create_form.html'
model = User
fields = ['username', 'password', 'email']
success_url = reverse('words:main_page')
And got a mistake
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/resolvers.py", line 600, in url_patterns
iter(patterns)
TypeError: 'module' object is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/linuxbrew/.linuxbrew/opt/python#3.9/lib/python3.9/threading.py", line 954, in _bootstrap_inner
self.run()
File "/home/linuxbrew/.linuxbrew/opt/python#3.9/lib/python3.9/threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/core/management/base.py", line 419, in check
all_issues = checks.run_checks(
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/core/checks/registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "/home/linuxbrew/.linuxbrew/opt/python#3.9/lib/python3.9/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 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/yegor/projects/word_collector/word_collector/urls.py", line 23, in <module>
path('', include('words.urls'))
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/home/linuxbrew/.linuxbrew/opt/python#3.9/lib/python3.9/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 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/yegor/projects/word_collector/words/urls.py", line 2, in <module>
from words.views import MainView, RegisterView, LoginView
File "/home/yegor/projects/word_collector/words/views.py", line 12, in <module>
class RegisterView(CreateView):
File "/home/yegor/projects/word_collector/words/views.py", line 17, in RegisterView
success_url = reverse('words:main_page')
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/base.py", line 54, in reverse
app_list = resolver.app_dict[ns]
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/resolvers.py", line 526, in app_dict
self._populate()
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/resolvers.py", line 460, in _populate
for url_pattern in reversed(self.url_patterns):
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/yegor/.cache/pypoetry/virtualenvs/word-collector-g9LS8W6v-py3.9/lib/python3.9/site-packages/django/urls/resolvers.py", line 607, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e
django.core.exceptions.ImproperlyConfigured: The included URLconf 'word_collector.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I'm sure that urlconf is great. Where this thing getting circular import? And how I should use success_url?
By the way, if I assign POST method to class -
def post(self, *args, **kwargs):
self.success_url = reverse('words:main_page')
return super().post(*args, **kwargs)
then everything work properly, but I still don't understand why?

Unable to use mixins from drf-extensions

I have installed drf-extensions and imported it to my views. After that, I started getting below exception and I was not able to identify the root cause of this. Can someone explain me, what went wrong here pls?
> python .\manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework\settings.py", line 177, in import_from_string
return import_string(val)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
module = import_module(module_path)
File "C:\Users\gknath\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:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\utils.py", line 6, in <module>
from rest_framework_extensions.key_constructor.constructors import (
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\key_constructor\constructors.py", line 4, in <module>
from rest_framework_extensions.key_constructor import bits
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\key_constructor\bits.py", line 3, in <module>
from django.db.models.sql.datastructures import EmptyResultSet
ImportError: cannot import name 'EmptyResultSet' from 'django.db.models.sql.datastructures'
(C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\db\models\sql\datastructures.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\gknath\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\gknath\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\Users\gknath\Desktop\python-django\django-models-rest\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:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\gknath\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:\Users\gknath\Desktop\python-django\testing_new_backend_model_modp\projects\urls.py", line 5, in <module>
from .api import router
File "C:\Users\gknath\Desktop\python-django\testing_new_backend_model_modp\projects\api.py", line 2, in <module>
from api.views import FactorViewSet, ProjectViewSet, FactorViewSet
File "C:\Users\gknath\Desktop\python-django\testing_new_backend_model_modp\api\views.py", line 3, in <module>
from rest_framework_extensions.mixins import NestedViewSetMixin
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\mixins.py", line 1, in <module>
from rest_framework_extensions.cache.mixins import CacheResponseMixin
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\cache\mixins.py", line 5, in <module>
class BaseCacheResponseMixin:
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework_extensions\cache\mixins.py", line 8, in BaseCacheResponseMixin
object_cache_key_func = extensions_api_settings.DEFAULT_OBJECT_CACHE_KEY_FUNC
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework\settings.py", line 225, in __getattr__
val = perform_import(val, attr)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework\settings.py", line 166, in perform_import
return import_from_string(val, setting_name)
File "C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\rest_framework\settings.py", line 180, in import_from_string
raise ImportError(msg)
ImportError: Could not import 'rest_framework_extensions.utils.default_object_cache_key_func' for API setting 'DEFAULT_OBJECT_CACHE_KEY_FUNC'. ImportError: cannot import name 'EmptyResultSet' from 'django.db.models.sql.datastructures' (C:\Users\gknath\Desktop\python-django\django-models-rest\venv\lib\site-packages\django\db\models\sql\datastructures.py).
If I disable the extension import then I can run fine. I am using three models and have written serializers and trying to make an nested API using drf_extensions.
I guess the problem is related to this issue: https://github.com/chibisov/drf-extensions/issues/294
Temporary fix for me was to uninstall and install with:
pip install -U git+git://github.com/chibisov/drf-extensions.git#8001a440c7322be26bbe2d16f3a334a8b0b5860b

Cannot Import Model Name Django with User Model and Informational Model

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

django Error: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet

I am getting an error in Django. I saw a lot of similar questions but am not able to find a working solution. I think the error is in importing the User model. I am not able to find a solution to this. Basically, it seems that it is not able to get the User model. Please help.
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "C:\Python34\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python34\lib\site-packages\django\apps\registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "C:\Python34\lib\site-packages\django\apps\config.py", line 90, in create
module = import_module(entry)
File "C:\Python34\lib\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 "E:\Python\TheRandomSite\investor\__init__.py", line 1, in <module>
from . import urls
File "E:\Python\TheRandomSite\investor\urls.py", line 18, in <module>
from . import views
File "E:\Python\TheRandomSite\investor\views.py", line 4, in <module>
from investor.models import Game
File "E:\Python\TheRandomSite\investor\models.py", line 3, in <module>
from django.contrib.auth.models import User
File "C:\Python34\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Python34\lib\site-packages\django\contrib\auth\base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "C:\Python34\lib\site-packages\django\db\models\base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Python34\lib\site-packages\django\apps\registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "C:\Python34\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.
Thanks.
Try this
Make investor/__init__.py as empty, Then
include('investor.urls')

AttributeError type object has no attribute

So I'm trying to open and print data from this JSON file using this code in my view:
from django.shortcuts import render
import json
class OnlicarView():
import json
from pprint import pprint
with open('C:/Python34/Scripts/onlicar/mytest/onlicar/static/json/vehicles.json') as data_file:
data = json.load(data_file)
pprint(data)
Here is my app url:
from django.conf.urls import url
from onlicar import views
urlpatterns = [
url(r'^onlicar/$', views.OnlicarView.as_view(), name='onlicar_signup'
)
]
Here is my root url:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^onlicar/', include('onlicar.urls'))
I am having trouble with the app url. I am getting an error saying that there is an Attribute Error and I don't know how to fix it.
Because I am getting the data from a JSON file there is nothing in my models at the moment. Am I meant to have something in the models?
My JSON file is printing now, but how do I render it to HTML instead?
Here is the error:
Unhandled exception in thread started by <function check_errors.<locals>.
at 0x03F9C4B0>
Traceback (most recent call last):
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\utils\autore
", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\managem
mands\runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\managem
e.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\managem
e.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\checks\
y.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\checks\
", line 14, in check_url_config
return check_resolver(resolver)
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\core\checks\
", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\utils\functi
", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\urls\resolve
line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_m
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\utils\functi
", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\urls\resolve
line 306, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python34\Scripts\onlicar\lib\importlib\__init__.py", line 109,
ort_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_unlo
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_re
File "C:\Python34\Scripts\onlicar\mytest\mytest\urls.py", line 21, in <
url(r'^onlicar/', include('onlicar.urls'))
File "C:\Python34\Scripts\onlicar\lib\site-packages\django\conf\urls\__
py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "C:\Python34\Scripts\onlicar\lib\importlib\__init__.py", line 109,
ort_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_unlo
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_re
File "C:\Python34\Scripts\onlicar\mytest\onlicar\urls.py", line 5, in <
url(r'^onlicar/$', views.OnlicarView.as_view(), name='onlicar_signup'
AttributeError: type object 'OnlicarView' has no attribute 'as_view'
There are a lot of things wrong with this code.
Firstly, you have declared this as a class, and used it in the URL as if it were a class-based view, but you do not inherit from any actual view class.
Just as seriously, in a class you do not put code at the class level; it needs to go inside a method.
Also, pprint will only print to the console, it will not display anything in the user's browser; you need to create a response and return that.

Categories