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_repr
fields = serializer.fields
File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py", line 312, in fields
for key, value in self.get_fields().items():
File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py", line 883, in get_fields
info = 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_info
reverse_relations = _get_reverse_relationships(opts)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/utils/model_meta.py", line 129, in _get_reverse_relationships
for 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_objects
include_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_model
self._fill_related_objects_cache()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 533, in _fill_related_objects_cache
for 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 wrapper
result = user_function(*args, **kwds)
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 168, in get_models
self.check_models_ready()
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 131, in check_models_ready
raise 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!
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With