django-seo setting up, models aren't loaded yet - django

I'm trying to add django-seo into my site. But I can't cope with setting up. I followed instructions documentation, but error occurs.
This is what I did:
Installed django-seo packacge
Added rollyourown.seo to INSTALED_APPS
Created seo.py file in my site content app
And this is what I wrote into seo.py file:
from rollyourown import seo
class Metadata(seo.Metadata):
title = seo.Tag(head=True, max_length=68)
description = seo.MetaTag(max_length=155)
keywords = seo.KeywordTag()
heading = seo.Tag(name="h1")
class Meta:
seo_views = ('SiteContent',)
seo_models = ('SiteContent',)
When Meta class is removed, I can't add any meta tags to contnet via Django Admin Site( I registered it in admin site ). I've read that django-seo use get_absolute_url() to deal with it. But in my site app I don't use this function for provide more some utilities to multilanguage.
But if i add Meta class, i will get this error:
Traceback (most recent call last):
File "F:/Site/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "F:\Python27\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
utility.execute()
File "F:\Python27\lib\site-packages\django\core\management\__init__.py", line 354, in execute
django.setup()
File "F:\Python27\lib\site-packages\django\__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "F:\Python27\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "F:\Python27\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "F:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\models.py", line 10, in <module>
__import__(module_name)
File "F:\Site\SiteContent\seo.py", line 5, in <module>
class Metadata(seo.Metadata):
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\base.py", line 166, in __new__
options = Options(Meta, help_text)
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\options.py", line 19, in __init__
self._set_seo_models(meta.pop('seo_models', []))
File "F:\Python27\lib\site-packages\djangoseo-1.0-py2.7.egg\rollyourown\seo\options.py", line 96, in _set_seo_models
seo_models.extend(models.get_models(app))
File "F:\Python27\lib\site-packages\django\db\models\__init__.py", line 54, in alias
return getattr(loading, function_name)(*args, **kwargs)
File "F:\Python27\lib\site-packages\django\utils\lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "F:\Python27\lib\site-packages\django\apps\registry.py", line 168, in get_models
self.check_models_ready()
File "F:\Python27\lib\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.
I tried diffrent solutions but nothing helped.

Django-SEO is not compatible with Django 1.7, which is the first version of Django that includes the AppRegistry.
Either rollback to Django 1.6.x or remove Django-SEO.

I got it working by changing get_query_set() to get_queryset()(changed in django1.8) in file rollyourown\seo\backends.py

As original Django-SEO is not supported anymore, these guys made and support their own version / fork of Django-SEO. I use it with Django 1.8.8, Python 3 is supported too.
https://github.com/whyflyru/django-seo

Related

'AppRegistryNotReady' error after Django Upgrade from 1.8 to 1.9 version

In our process of upgrading our Django project from 1.8 to 1.9 version, we removed all the deprecation warnings/errors according to Django-1.9 release notes.
But, after updating the Django version to 1.9 in the project's virtual environment, we got the following error:
Traceback (most recent call last):
File "/home/django/sites/site_dev/conf/site.wsgi", line 46, in application
return get_wsgi_application()(env, start_response_with_id)
File "/home/django/sites/site_dev/lib/python2.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
django.setup()
File "/home/django/sites/site_dev/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/django/sites/site_dev/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/django/sites/site_dev/lib/python2.7/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/apps.py", line 13, in <module>
from .app_name import update_language_preference
File "/home/accounts.py", line 4, in <module>
from app_name.models.core import UserAccountModel
File "/home/models/core.py", line 8, in <module>
class TimeStampedModel(models.Model):
File "/home/django/sites/site_dev/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "/home/django/sites/site_dev/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "/home/django/sites/site_dev/lib/python2.7/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.
We have many installed apps in the project and using PostgreSQL-9.5 database. We know that the apps which have model classes in it, cause this error to occur.
The above error even occured when connecting to the database using
connection cursor:
django.db.connection.cursor().execute()

AppRegistryNotReady: lazy format_html()?

Why do I get this exception?
Traceback (most recent call last):
File "/path1/myapp-isu/myapp_isu/tests/unit/views/test_view_isu.py", line 8, in <module>
from myapp_isu.search_form import ISUSearchForm
File "/path1/myapp-isu/myapp_isu/search_form.py", line 87, in <module>
class ISUSearchForm(forms.Form):
File "/path1/myapp-isu/myapp_isu/search_form.py", line 108, in ISUSearchForm
foo_filter=forms.ModelChoiceField(FooFilter.objects.all(), label=format_html('%s', reverse_lazy('foo-filter'), FooFilter._meta.verbose_name))
File "/path1/dt/dt/utils/templateutils.py", line 127, in reverse
return urlresolvers.reverse(*args, **kwargs)
File "/path1/dt/dt/utils/urlresolverutils.py", line 49, in patched_reverse
base_url = orig_reverse(viewname, urlconf=urlconf, args=args, kwargs=kwargs, prefix=prefix, current_app=current_app)
File "/path2/django/core/urlresolvers.py", line 578, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/path2/django/core/urlresolvers.py", line 432, in _reverse_with_prefix
self._populate()
File "/path2/django/core/urlresolvers.py", line 284, in _populate
for pattern in reversed(self.url_patterns):
File "/path2/django/core/urlresolvers.py", line 401, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/path2/django/core/urlresolvers.py", line 395, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/path1/myapp-eins/myapp_eins/etc/rooturls.py", line 13, in <module>
admin.autodiscover()
File "/path2/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/path2/django/utils/module_loading.py", line 67, in autodiscover_modules
for app_config in apps.get_app_configs():
File "/path2/django/apps/registry.py", line 137, in get_app_configs
self.check_apps_ready()
File "/path2/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.
It only happens if I call the unittest via PyCharm, not if I use py.test on the shell.
I guess reverse_lazy() is not lazy here, since it gets used in format_html(). Any way to have a lazy format_html()?
Versions:
Django 1.8
Pycharm 5.0.4
Since there are things like url_patterns in the stacktrace, I assume that DJANGO_SETTINGS_MODULE is set correctly .
But, you still need to call django.setup() before running anything else.
import django
django.setup()
For me, that made this error message go away.
I've had some trouble using PyCharm, myself, and I'll assume you're using the the community edition (which I've been using).
If so, the problem is very likely that you're not properly configured for django. You can probably fix this with some hacks that might work for this.
I'd start off with this.
(importing django to ensure the django console is run)
Then maybe this.
There's also this:
Check "Edit Configurations" under the test you're running, and add DJANGO_SETTINGS_MODULE=<app-name-here>.settings to environment variables.
If all of the above fails, try initializing your form and doing the import in the constructor:
class ISUSearchForm(...):
...
foo_filter = forms.Field()
...
def __init__(*args, **kwargs)
from ... import reverse_lazy
from ... import FooFilter
self.fields['foo_filter'] = \
forms.ModelChoiceField(
FooFilter.objects.all(),
label=format_html(
'%s',
reverse_lazy('foo-filter'),
FooFilter._meta.verbose_name
)
)
super().__init__(*args, **kwargs)
The error may be caused by the exact import sequence performed by the different test runners and the fact that you use only class variables to customize your form.
In django 1.8, reverse() and reverse_lazy() now return Unicode strings instead of byte strings.
Please refer the link:
https://docs.djangoproject.com/en/1.8/releases/1.8/

migrate failure (django 1.8)

I'm trying in vain to migrate my database to the server. here is the traceback that I get from the terminal.
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv) File "/home/guimatsi/webapps/nnmland/lib/python2.7/Django-1.8.6-py2.7.egg/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute() File "/home/guimatsi/webapps/nnmland/lib/python2.7/Django-1.8.6-py2.7.egg/django/core/management/__init__.py", line 328, in execute
django.setup() File "/home/guimatsi/webapps/nnmland/lib/python2.7/Django-1.8.6-py2.7.egg/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS) File "/home/guimatsi/webapps/nnmland/lib/python2.7/Django-1.8.6-py2.7.egg/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models) File "/home/guimatsi/webapps/nnmland/lib/python2.7/Django-1.8.6-py2.7.egg/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name) File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name) File "/home/guimatsi/lib/python2.7/registration/models.py", line 15, in <module>
User = get_user_model() File "/home/guimatsi/webapps/nnmland/lib/python2.7/Django-1.8.6-py2.7.egg/django/contrib/auth/__init__.py", line 150, in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL) File "/home/guimatsi/webapps/nnmland/lib/python2.7/Django-1.8.6-py2.7.egg/django/apps/registry.py", line 199, in get_model
self.check_models_ready() File "/home/guimatsi/webapps/nnmland/lib/python2.7/Django-1.8.6-py2.7.egg/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.
Since I'm new in django and it's my first project, Every hint would be appreciated. ths in advance.
It looks like you might be using a version of Django registration that does not support Django 1.8. You could try installing Django registration redux instead.

Django 1.7 upgrade error: AppRegistryNotReady using serializers from rest_framework

I get this traceback:
Traceback (most recent call last):
File "./manage.py", line 38, in <module>
execute_from_command_line(sys.argv)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/mgregory/Documents/cm_central/cmh_server/models.py", line 88, in <module>
class VersionSerializer(serializers.ModelSerializer):
File "/Users/mgregory/Documents/cm_central/cmh_server/models.py", line 89, in VersionSerializer
brzs= BrzSerializer(many=True)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 198, in __init__
self.fields = self.get_fields()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 234, in get_fields
default_fields = self.get_default_fields()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 732, in get_default_fields
reverse_rels = opts.get_all_related_objects()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 498, in get_all_related_objects
include_proxy_eq=include_proxy_eq)]
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 510, in get_all_related_objects_with_model
self._fill_related_objects_cache()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-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 "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
self.check_models_ready()
File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/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.
from this code (models.py):
# Serializers for transmitting CMx install information over HTTP
class BrzSerializer(serializers.ModelSerializer):
class Meta:
model = Brz
fields=('filename',)
class VersionSerializer(serializers.ModelSerializer):
brzs= BrzSerializer(many=True)
class Meta:
model = Version
fields=('name', 'for_mac', 'for_windows', 'brzs')
It sounds like it's telling me "your VersionSerializer can't have a BrzSerializer, because you haven't registered that yet".
I've looked at other SO questions relating to AppRegisteryNotReady, but didn't find one that matches this symptom. Surely I have to be able to define a chain of dependent models like this?
It turns out that having the declaration of the serializers inside models.py is causing that application to be used before the app registry has finished being loaded.
models.py is actually the wrong place to declare these serializers (though I'm 99% certain I did it that way based on an example of how to use them).
The fix is to move the declaration of the serializers out into their own file (which makes sense, because they have nothing to do with the database schema, which models.py is defining), and import that from the view. By the time the view gets going, the app registry is ready.

./manage.py test of Django 1.4 gives a Thing2Literal import not found on google appengine

I followed the steps here https://developers.google.com/cloud-sql/docs/django and it ran well with django 1.3.1. Now up to Django 1.4 and gives a funny stack trace. I would paste the relevant part of the message here
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/dumb906/woody/py/mdlr/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/dumb906/woody/py/mdlr/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/dumb906/woody/py/mdlr/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/dumb906/woody/py/mdlr/django/core/management/__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/home/dumb906/woody/py/mdlr/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/dumb906/woody/py/mdlr/django/core/management/commands/test.py", line 7, in <module>
from django.test.utils import get_runner
File "/home/dumb906/woody/py/mdlr/django/test/__init__.py", line 5, in <module>
from django.test.client import Client, RequestFactory
File "/home/dumb906/woody/py/mdlr/django/test/client.py", line 21, in <module>
from django.test import signals
File "/home/dumb906/woody/py/mdlr/django/test/signals.py", line 2, in <module>
from django.db import connections
File "/home/dumb906/woody/py/mdlr/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/home/dumb906/woody/py/mdlr/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/dumb906/woody/py/mdlr/django/db/utils.py", line 92, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/dumb906/woody/py/mdlr/django/db/utils.py", line 44, in load_backend
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'google.appengine.ext.django.backends.rdbms' isn't an available database backend.
Try using django.db.backends.XXX, where XXX is one of:
'dummy', 'mysql', 'oracle', 'postgresql_psycopg2', 'sqlite3'
Error was: cannot import name Thing2Literal
Any help? Some one said it needs to be compiled http://django-irc-logs.com/2012/mar/27/ ?
I had the same issue. Since the problem is with importing Thing2Literal that is used by adapt_datetime_with_timezone_support function from django/db/backends/mysql/base.py and according to this:
https://code.djangoproject.com/changeset/17596/django/trunk/django/db/backends/mysql/base.py this is important only to datetime objects that bypass the model layer and are used with raw sql. So i decided it is not important for me and I messed a bit my django/db/backends/mysql/base.py: comment out code that causes trouble (import of Thing2Literal, adapt_datetime_with_timezone_support function and line 83 where the function is called)
Of course I upload my django 1.4 customized that way to appengine together with my project and it works.
I would appreciate feedback from those who understand django internals better, whether what i've done is ok assuming i don't use raw sql at all.
As you can see in the documentation you've linked, Google App Engine support only Django up to version 1.3 (actually 1.3.1).