59

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??

djvg
  • 11,722
  • 5
  • 72
  • 103
alanjds
  • 3,972
  • 2
  • 35
  • 43
  • 2
    Don't run commands from `django-admin` - use `manage.py`, which sets your settings module explicitly. – Daniel Roseman Apr 10 '15 at 15:52
  • Tried using `manage.py`. Same result. – alanjds Apr 10 '15 at 17:57
  • ...anyway, the DJANGO_SETTINGS_MODULE is set correctly on the environment – alanjds Apr 10 '15 at 18:08
  • The root cause of `LookupError` highly depends on your custom migration code, e.g. I also hit the issue then figured out that I used improper version of `apps` (the one provided by `pre_migrate` signal receiver) instead of the one provided by `RunPython` forwards function ..... You should take great care when dealing with multiple `state` and `apps` in your migration code. – Ham Jan 19 '21 at 17:02

18 Answers18

60

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),
    ]
Rockallite
  • 16,437
  • 7
  • 54
  • 48
  • 2
    Thanks! It's worth to mention that this happened to me only while running tests. – David Viktora Sep 26 '18 at 14:11
  • problem still exists! I need to use this solution in docker-compose project. so, what a i suppose to do with semi-new django project with command line CMD [ "python", "manage.py", "runserver", "0.0.0.0:3000" ]??! – Mahdi Imani Apr 21 '19 at 10:27
40

Just check your settings.py in the project directory and make sure that you have added your app there :

# Application definition

    INSTALLED_APPS = [
        # My Apps
        '......'

        # Default Apps 
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
]
MoShamroukh
  • 785
  • 7
  • 7
  • 1
    This worked for me. I don't why `python mangage.py startapp xyz` did not do this! – NurShomik Oct 26 '21 at 00:44
  • 1
    Well, apps were installed correctly on my project. But good to know that some people got helped by this answer anyway. – alanjds Jan 19 '23 at 17:51
9

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.

JV.
  • 2,658
  • 4
  • 24
  • 36
  • 1
    For cross-app migration, it makes sense require explicit dependency. Otherwise running migrations on empty database in alphabetical order would fail. – Pawel Furmaniak Sep 14 '17 at 16:24
  • Thanks, ran into the same issue as posted by OP while running migrations. The dependencies of the Migration class was not set. After setting it appropriately, the error went away. dependencies = [ ('loginmodule', '0002_login_avatar'), ] – Binita Bharati Feb 05 '19 at 10:00
3

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
Sibin John Mattappallil
  • 1,739
  • 4
  • 23
  • 37
  • 1
    I also got this error when I didn't have a requirement installed. It was totally unrelated to django.contrib.admin – Jake Apr 30 '19 at 03:33
2

I was getting a similar error and I am a total novice programmer. One solution that worked for me was installing sqlparse. Try

pip install sqlparse

2

I also had this same error of "no installed app label 'admin' ". I was able to solve it by running the pip install sqlparse command

2

In my case, app name wasn't added to the settings.py file under "INSTALLED APPS" dictionary

tsadigov
  • 153
  • 1
  • 7
  • Please don't "add on" to questions in an answer. Wait until you have enough reputation then add this as a comment. – M-Chen-3 Dec 07 '20 at 16:38
1

Try looking further up your stack trace too. I got this error due to a misconfigured logger but I had to look further up the trace to find this issue!

In my case I had misnamed my environment variable DJANGO_LOG_LEVL as DEGUB instead of DEBUG (note the misspelling) and that caused the error.

user1847
  • 3,571
  • 1
  • 26
  • 35
1

On the Installed Applications, make sure you have the app. For instance, I have Polls under the settings.py file.

Application definition

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
0

For me it shows

raise LookupError(message)

LookupError: No installed app with label 'admin'.

and I solve it by pip installing every requirements manually I m using ubuntu 16.04

ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
Yuvaraj K
  • 9
  • 1
0

For my case the LookupError was occuring because I had altered the models and added 'related_name' but had not run makemigrations and migrate.

0

I got the error when I used 'required=False' in my model like this: slug = models.SlugField(required=False)

I changed it to: slug = models.SlugField(blank=True,null=True) and the error disappeared

  • The second way you did this is accurate though. The first one is not, those attributes in the model field normally directly associate to database settings on a field. `blank=True` allows you to not have the field entered in a model form, but `null=True` allows the field to be null. – ViaTech Jan 24 '22 at 12:53
0

I also got this error after changing database from sqlite to postgresql and by installing psycog2 (which is used to connect django to postgre database) my error has gone.

pip install psycog2

0

Had troubles with this too, all I did was upgrade pip and it seemed to fix itself tried installing other dependencies but turned out I already had them all so if you are in the same position try upgrading your pip!

36Boxes
  • 53
  • 7
0

The same thing also happened to me, but in my case the INSTALLED_APPS variable had been overwritten a few lines below in this file in the settings.py file.

  • Why would your `INSTALLED_APPS` setting ever be dynamic, you have to restart the server to load them? This sounds like bad practice if this is what you are doing and that is all that makes sense in my head for an app getting overridden in settings.py – ViaTech Jan 24 '22 at 12:49
0

create a virtual environment and install all the required dependency or packages in virtual environment

Aashish Kumar
  • 111
  • 1
  • 5
0

I had faced the same problem. But after some analysis, I found that I didn't put the newly created app name into my setting.py.when I placed it then it worked. Your setting.py will look like this one :

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

You need to do makemigrations n migrate after you will not find this type of error