Django migrate with zinnia- InvalidBasesError: Cannot resolve ba

ghz 13hours ago ⋅ 4 views

Django migrate with zinnia- InvalidBasesError: Cannot resolve bases for [<ModelState: 'zinnia.Author'>]

I am having some problems adding a zinnia blog app to an existing django application I have. It's been a couple of days with no results, so I'm open to any and all ideas.

I am getting a similar error to this github issue on python 3.4.0 django 1.7.5. This is the output of migrate:

(project)username@computername:~/project/application$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: bootstrap3
  Apply all migrations: project, activity, zinnia, sessions, auth, badge, admin, coupon, contenttypes, sites
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations: Applying zinnia.0001_initial...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
    if self.detect_soft_applied(migration):
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/db/migrations/executor.py", line 140, in detect_soft_applied
    apps = project_state.render()
  File "/home/username/.virtualenvs/project/local/lib/python3.4/site-packages/django/db/migrations/state.py", line 75, in render
    "for more" % new_unrendered_models
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'zinnia.Author'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
 in an app with no migrations; see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more

Again, I'm using python 3.4.0 django 1.7.5 and latest versions of zinnia (0.15.1).

This is the output of my pip freeze: (as I said, its an existing app so I have other packages installed for it)

beautifulsoup4==4.3.2
Django==1.7.5
django-blog-zinnia==0.15.1
django-bootstrap3==5.1.1
django-braces==1.4.0
django-contrib-comments==1.5
django-mptt==0.6.1
django-tagging==0.3.4
django-xmlrpc==0.1.5
enum34==1.0.4
flake8==2.3.0
ipaddress==1.0.7
ipython==3.0.0
isort==3.9.6
jedi==0.8.1
mccabe==0.3
mots-vides==2015.2.6
natsort==3.5.2
pathlib==1.0.1
pbr==0.10.7
pep8==1.6.2
pep8-naming==0.2.2
pies==2.6.3
Pillow==2.7.0
psycopg2==2.6
pyflakes==0.8.1
pyparsing==2.0.3
python3-memcached==1.51
pytz==2014.10
raven==5.2.0
requests==2.5.3
six==1.9.0
stevedore==1.2.0
virtualenv==12.0.7
virtualenv-clone==0.2.5
virtualenvwrapper==4.3.2

edit: I have a feeling this has to do with a model I have in my application which has from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManagerBut I can't imagine not using that in my app. Maybe it's time to look for another blog package.

Answer

It seems like you're encountering an issue related to migrations in Django, specifically when trying to integrate the Zinnia blog app with your existing Django application. The error InvalidBasesError: Cannot resolve bases for [<ModelState: 'zinnia.Author'>] typically occurs when Django cannot resolve the inheritance hierarchy of models, possibly due to mismatches or missing migrations in one of the apps (like django.contrib.auth).

Here are some troubleshooting steps and potential solutions you can try:

1. Ensure Migrations Are Created for All Apps:

The error suggests that Django is having trouble resolving the Author model in Zinnia due to a potential migration problem. Ensure that you have migrations for all apps in your project, including Zinnia and the auth app.

Steps to check:

  • Run python manage.py makemigrations for each app, especially for zinnia and your app that extends AbstractBaseUser.
  • After ensuring migrations exist for all apps, run python manage.py migrate again to apply them.

If you see no new migrations or a warning, try deleting migration files from the migrations folder of any app that might have corrupted migrations and then re-run makemigrations to regenerate them.

2. Check Your Custom User Model:

You mentioned using AbstractBaseUser, which means you are implementing a custom user model. This can sometimes conflict with other apps that expect User to be in the django.contrib.auth app (like Zinnia).

Steps to check:

  • Ensure that your custom user model is correctly specified in your settings.py:

    AUTH_USER_MODEL = 'yourapp.CustomUser'  # Use your actual model name
    
  • If you are using a custom user model, make sure it is defined before running the migration for the first time. The custom user model should be set during the initial migration of your project. If you change the AUTH_USER_MODEL after migrations have already been applied, this could cause migration issues.

  • If you're using PermissionsMixin, ensure that it is correctly inherited in your custom user model.

3. Ensure Dependencies for Zinnia Are Met:

Zinnia uses several dependencies that need to be present for migrations to apply correctly. Ensure the following apps are installed and migrated:

  • django.contrib.auth (for user and permissions models)
  • django.contrib.contenttypes
  • django.contrib.sites

You can check if these apps are listed in INSTALLED_APPS in settings.py.

4. Fixing the Migrations Issue:

The error you're seeing (InvalidBasesError: Cannot resolve bases) may also suggest that Zinnia's migrations are trying to reference a model (in your case, zinnia.Author) that inherits from a model that hasn't been fully migrated or is missing in your project.

Steps to fix:

  • Reset migrations: If you can afford to, try resetting migrations:

    1. Delete all migration files (except __init__.py) from each app’s migrations folder.
    2. Run python manage.py makemigrations for all apps.
    3. Run python manage.py migrate to reapply migrations.
  • Fake migrations: If you're in a situation where migrations have been applied but the schema is already in place (for example, if the tables exist), you can try "faking" the migrations:

    python manage.py migrate --fake zinnia
    

This will mark the migrations as applied without actually applying them. However, be cautious when using this approach, as it can result in inconsistencies if the schema isn't actually correct.

5. Check for Django Version Compatibility:

You're using Django 1.7.5, which is quite old. Zinnia 0.15.1 was released around the time Django 1.7 was popular, but it's still good practice to check for compatibility with newer versions.

If you are not tied to Django 1.7, consider upgrading to a newer version of Django (e.g., Django 1.8 or higher) if possible. This can often resolve issues caused by outdated libraries or migrations.

6. Check Zinnia's Migrations:

Zinnia has its own migrations starting from version 0.15. Ensure that the migrations provided by Zinnia are correctly applied.

Steps to ensure Zinnia migrations are applied:

  • Check the migration files inside zinnia/migrations directory.
  • If you suspect a migration issue, you can manually inspect the zinnia.0001_initial migration and see what changes it's trying to apply.
  • Ensure that zinnia is added to the INSTALLED_APPS in your settings.py file.

7. Use the --run-syncdb Option:

Sometimes, when a migration fails and creates a problematic state, you can try running:

python manage.py migrate --run-syncdb

This option syncs the database, applying all missing tables that haven’t been migrated yet.

8. Zinnia’s Dependencies:

Zinnia depends on django-mptt, django-tagging, and django-contrib-comments. Ensure these dependencies are installed and up to date:

pip install django-mptt django-tagging django-contrib-comments

After installing, run python manage.py migrate again.

9. Possible Workaround: Use a Different Blog App:

If the above steps don't resolve your issue and you're still stuck with Zinnia, you might consider switching to a different Django blog app, such as:

  • Django-blog: A lightweight Django blogging app.
  • Wagtail: A powerful and flexible CMS for Django (supports blogging as well).

These alternatives may be easier to integrate depending on your needs and the version of Django you're using.

Summary:

  1. Ensure all migrations are created and applied for Zinnia and your custom user model.
  2. Check that the required dependencies for Zinnia are installed (django-mptt, django-tagging, etc.).
  3. If using a custom user model, ensure AUTH_USER_MODEL is set correctly.
  4. Try resetting migrations if necessary or use the --fake flag for migration conflicts.
  5. Consider upgrading Django or using an alternative blog app if the issue persists.

Let me know if any of these steps help or if you run into additional issues!