djangodjango-migrationsdjango-managersdjango-sites

Django Migrations ValueError: Could not find manager in django.db.models.manager


I'm trying to update from Django 1.7 to Django 1.8

One of my models uses CurrentSiteManager from django.contrib.sites.managers like so:

from django.contrib.sites.managers import CurrentSiteManager

class NewsQuerySet(models.QuerySet):
    #...

class News(models.Model):
    #...

    objects = NewsQuerySet.as_manager()
    on_site = CurrentSiteManager.from_queryset(NewsQuerySet)()

When I try to run migrations (makemigrations or migrate) I get:

ValueError: Could not find manager CurrentSiteManagerFromNewsQuerySet in django.db.models.manager. Please note that you need to inherit from managers you dynamically generated with 'from_queryset()'.

If I remove the on_site manager, everything works fine.

Any ideas how to overcome this?


Solution

  • Turns out, since Django 1.8 we can serialize Managers using use_in_migrations.

    And the CurrentSiteManager is marked with use_in_migrations = True

    So the fix is to set back use_in_migrations = False. I did it this way:

    class NewsSiteManager(CurrentSiteManager.from_queryset(NewsQuerySet)):
        use_in_migrations = False
    
    
    class News(models.Model):
        #...
    
        objects = NewsQuerySet.as_manager()
        on_site = NewsSiteManager()