Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No installed app with label 'admin'" running Django migration. The app is installed correctly

I am trying to use admin.LogEntry objects during a datamigration on Django 1.7

The 'django.contrib.admin' app is listed on INSTALLED_APPS.

On the shell, it works:

>>> from django.apps import apps
>>> apps.get_model('admin', 'LogEntry')
django.contrib.admin.models.LogEntry

But during the migration, it fails:

def do_it(apps, schema_editor):
    LogEntry = apps.get_model('admin', 'LogEntry')

Fails like this:

django-admin migrate
(...)
LookupError: No installed app with label 'admin'.

Using a debugger, I got that the 'admin' is not installed:

ipdb> apps.get_apps()
[]
ipdb> apps.all_models.keys()
['website', 'google', 'allauth', 'twitter', 'busca', 'conteudo', 'django_mobile', 'django_filters', 'videocenter', 'tinymce', 'oferta', 'programacaotv', 'contenttypes', 'suit', 'haystack', 'destaque', 'filer', 'galeria', 'auth', 'facebook', 'paintstore', 'critica', 'disqus', 'fichas', 'omeletop', 'autocomplete_light', 'modelsv1', 'temas', 'django_extensions', 'adv_cache_tag', 'taggit', 'social', 'personalidade']

WHY??

like image 290
alanjds Avatar asked Apr 10 '15 15:04

alanjds


People also ask

How do I see installed apps in Django?

The list of installed applications is defined in settings. INSTALLED_APPS . It contains a tuple of strings, so you can iterate on it to access each application's name.

Why Makemigrations is not working?

This may happen due to the following reasons: You did not add the app in INSTALLED_APPS list in settings.py (You have to add either the app name or the dotted path to the subclass of AppConfig in apps.py in the app folder, depending on the version of django you are using). Refer documentation: INSTALLED_APPS.


4 Answers

The Django doc makes it clear:

When writing a RunPython function that uses models from apps other than the one in which the migration is located, the migration’s dependencies attribute should include the latest migration of each app that is involved, otherwise you may get an error similar to: LookupError: No installed app with label 'myappname' when you try to retrieve the model in the RunPython function using apps.get_model().

Code example:

# Imports are omitted for the sake of brevity

def move_m1(apps, schema_editor):
    LogEntry = apps.get('admin.logentry')
    # Other business logic here ...


class Migration(migrations.Migration):

    dependencies = [
        ('app1', '0001_initial'),

        # Below is the manually added dependency, so as to retrieve models
        # of 'django.contrib.admin' app with apps.get_model() in move_m1().
        #
        # Currently this is for Django 1.11. You need to look into
        # 'django/contrib/admin/migrations' directory to find out which is
        # the latest migration for other version of Django.
        ('admin', '0002_logentry_remove_auto_add'),
    ]

    operations = [
        migrations.RunPython(move_m1),
    ]
like image 184
Rockallite Avatar answered Oct 17 '22 15:10

Rockallite


Just check your setting.py in the installed apps section and make sure that you have added your app there :

# Application definition

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',


        '--- you need to add your app here ---',
    ]
like image 30
MoShamroukh Avatar answered Oct 17 '22 15:10

MoShamroukh


I don't know the exact cause for this. Will have to dig into the source code. but for now a workaround is add ('admin', 'name_of_last_migration_in_admin_app') to the dependencies and the migrations shall go alright.

like image 40
JV. Avatar answered Oct 17 '22 13:10

JV.


I got the same error (but unrelated to the issue mentioned in question). I was using mysql db but there were no mysql client.

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        # other details like name, user, host
    }
}

I installed mysqlclient (In ubuntu & Python3):

sudo apt-get install libmysqlclient-dev
sudo apt-get install python3-dev
pip install mysqlclient
like image 33
Sibin John Mattappallil Avatar answered Oct 17 '22 14:10

Sibin John Mattappallil