Pycharm 3.4.1 - AppRegistryNotReady: Models arent loaded yet. Django Rest framewrok

2024/10/8 12:37:53

I'm using DRF and Pycharm 3.4.1 and Django 1.7. When I try to test my serializer class via Pycharm django console, it gives me the following error:

Code

from items_app.serializers import ItemSerializer
s = ItemSerializer()
print(repr(s))

then cause the following error traceback:

Traceback

Traceback (most recent call last):File "<input>", line 1, in <module>File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py", line 440, in __repr__return unicode_to_repr(representation.serializer_repr(self, indent=1))File "/usr/local/lib/python2.7/dist-packages/rest_framework/utils/representation.py", line 75, in serializer_reprfields = serializer.fieldsFile "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py", line 312, in fieldsfor key, value in self.get_fields().items():File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py", line 883, in get_fieldsinfo = model_meta.get_field_info(model)File "/usr/local/lib/python2.7/dist-packages/rest_framework/utils/model_meta.py", line 68, in get_field_inforeverse_relations = _get_reverse_relationships(opts)File "/usr/local/lib/python2.7/dist-packages/rest_framework/utils/model_meta.py", line 129, in _get_reverse_relationshipsfor relation in opts.get_all_related_objects():File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 498, in get_all_related_objectsinclude_proxy_eq=include_proxy_eq)]File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 510, in get_all_related_objects_with_modelself._fill_related_objects_cache()File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 533, in _fill_related_objects_cachefor klass in self.apps.get_models(include_auto_created=True):File "/usr/local/lib/python2.7/dist-packages/django/utils/lru_cache.py", line 101, in wrapperresult = user_function(*args, **kwds)File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 168, in get_modelsself.check_models_ready()File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 131, in check_models_readyraise AppRegistryNotReady("Models aren't loaded yet.")
AppRegistryNotReady: Models aren't loaded yet.

but when I'm using my terminal(instead of pycharm django console), It works properly! I know there is a problem with Pycharm but I don't know how to fix it!

Answer

Loading the app registry is part of the django.setup method. If the app registry is not loaded when you start using the console, the most likely reason is that it is a plain python console instead of a fully blown Django console.

Try the following code. If that solves it, you are indeed using a plain python console.

>>> import django
>>> django.setup()
https://en.xdnf.cn/q/70119.html

Related Q&A

Pass Flask route parameters into a decorator

I have written a decorator that attempts to check we have post data for a Flask POST route:Heres my decorator:def require_post_data(required_fields=None):def decorator(f):@wraps(f)def decorated_functio…

update env variable on notebook in VsCode

I’m working on a python project with a notebook and .env file on VsCode. I have problem when trying to refresh environment variables in a notebook (I found a way but its super tricky). My project: .en…

How do I properly set up flask-admin views with using an application factory?

Im trying to setup flask-admin model views with SQLAlchemy against user and role models. Instead of a function admin view Im getting:ValueError: Invalid model property name <class app.models.Role>…

Django Rest Framework: Correct way to serialize ListFields

Based on the DRF documentation I have a created a list of email_id stored in my model in the following way Models.pyclass UserData(models.Model):emails = models.CharField(max_length=100,blank=False)In…

Flask-SQLAlchemy TimeoutError

My backend configuration is :Ubuntu 12.04 Python 2.7 Flask 0.9 Flask-SQLAlchemy Postgres 9.2Ive got this error message: TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed ou…

Making saxon-c available in Python

I have just read that Saxon is now available for Python, and thats just great fun and good, but can anyone write a tutorial on how to make it available for Python/Anaconda/WingIDE or similar? I am use…

How to include multiple interactive widgets in the same cell in Jupyter notebook

My goal is to have one cell in Jupyter notebook displaying multiple interactive widgets. Specifically, I would like to have four slider for cropping an image and then another separate slider for rotati…

SWIG - Problem with namespaces

Im having trouble getting the following simple example to work with SWIG 1.3.40 (and I also tried 1.3.31). The Foo structure comes through as a Python module as long as I dont wrap it in a namespace, b…

Django access to subclasses items from abstract class

class Animal(models.Model):....class Meta:abstract = Trueclass Cat(models.Model, Animal):...class Dog(models.Model, Animal):....I want to be able to return all instances of querysets of all the subclas…

Django BinaryField retrieved as memory position?

Ive written a short unit test with the following code:my_object = MyObject() my_object.data = b12345my_object.save() saved_object = MyObject.objects.first()assert saved_object.data == my_object.datawhe…