2

A source Django 3 by examples Chapter 14

When I try to run python manage.py migrate --settings=educa.settings.pro

Another files are copies and pastes from the book

The result is

  File "C:\Python\educa\manage.py", line 22, in <module>
    main()
  File "C:\Python\educa\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
    schema_editor.add_field(
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\base\schema.py", line 487, in add_field
    if field.many_to_many and field.remote_field.through._meta.auto_created:
AttributeError: 'str' object has no attribute '_meta'

Migration file:

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('account', '0005_contant'),
    ]

    operations = [
        migrations.CreateModel(
            name='Contact',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=True, verbose_name='ID')),
                ('user_from', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rel_from_set', to=settings.AUTH_USER_MODEL)),
                ('user_to', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rel_to_set', to=settings.AUTH_USER_MODEL)),
                ('created', models.DateTimeField(auto_now_add=True, db_index=True)),
            ],
            options={
                'ordering': ('-created',),
            },
        ),
    ]

Please help me, I don't know what to do.

FakeJavano
  • 21
  • 1

3 Answers3

0

If you provide the code for your models.py file, that would help. However, a good reason for this type of error is when you're creating a through table for a many to many join and the through model has no __str__ method in it's Meta class. If the method exists check that the indentation is correct.

0

I also had this kind of error when I was reading that book. Actually problem is with django version. The book used django==3.0.4 Check if you are using the right version. This error occurrs if you are using recent versions of django especially 3.2 or above.

In order to remove the migration file of the auth models remove the django via pip uninstall django or remove file manually by finding the path django/contrib/auth/migrations/migration_file_name

After removing the old migration files regenerate them with right version of django and migrate them.

I hope this solves your issue too)

0

One cause is that the specified through field model does not exist. This is usually caused by a missing migration dependency. In that case the model exists (since the generated migration refers to it) but its migration wasn't applied yet. (hence does not exist as a historical migration model that you can refer to yet)

Andreas
  • 970
  • 18
  • 28