attributeError:' str'对象没有属性' _Meta'迁移

发布于 2025-01-24 19:56:39 字数 4309 浏览 2 评论 0原文

我将库存模型移至自己的应用程序中,并重置所有迁移和数据库。 python manage.py makemigrations没有问题,但是迁移时我会遇到错误。希望有人可以将我指向一个方向,因为追溯似乎对我解决问题并不有帮助。

*由于堆栈溢出限制,我删除了代码的某些部分和追溯

django版本4.0.2

trackback

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, inventory, item, project_site, requisition, sessions, transfer, users
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  ...
  Applying item.0001_initial... OK
  Applying project_site.0001_initial...Traceback (most recent call last):
  File "C:\Users\Bernard\pelicans\imrs-capstone\imrs\manage.py", line 22, in <module>
    main()
  File "C:\Users\Bernard\pelicans\imrs-capstone\imrs\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Bernard\pelicans\imrs-capstone\env\lib\site-packages\django\core\management\__init__.py", line 
425, in execute_from_command_line
    utility.execute()
...
  File "C:\Users\Bernard\pelicans\imrs-capstone\env\lib\site-packages\django\db\migrations\operations\models.py", line 92, in database_forwards
    schema_editor.create_model(model)
  File "C:\Users\Bernard\pelicans\imrs-capstone\env\lib\site-packages\django\db\backends\base\schema.py", line 364, in create_model
    if field.remote_field.through._meta.auto_created:
AttributeError: 'str' object has no attribute '_meta'

project_site/migrations/migrations/0001_initial.py.py.py

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('item', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Cart',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('cartItemCount', models.PositiveIntegerField(default=0)),
            ],
        ),
        migrations.CreateModel(
            name='Site',
            fields=[
                ...
                ('inventory_items', models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')),
                ('siteCart', models.ManyToManyField(blank=True, related_name='siteCart', through='project_site.Cart', to='item.Item')),
            ],
        ),
    ]

project_site/models.py.pypy.py

class Site(models.Model):
    siteCart = models.ManyToManyField(
        'item.Item',
        through='Cart',
        through_fields=('site', 'cartItem'),
        blank=True,
        related_name='siteCart')
    inventory_items = models.ManyToManyField(
        'item.Item',
        through='inventory.Inventory',
        through_fields=('site', 'item'),
        blank=True,
        related_name='inventory_items'
        )

库存/models.py

class Inventory(models.Model):
    item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
    site = models.ForeignKey(Site, on_delete=models.CASCADE)
    ...

库存/迁移/0001_Initial.py

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('item', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Inventory',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('siteItemCount', models.PositiveIntegerField(default=0)),
                ('siteItemStatus', models.PositiveSmallIntegerField(choices=[(0, 'Above Threshold'), (1, 'Moderate'), (2, 'Low'), (3, 'Critical'), (4, 'Empty')], default=1)),
                ('siteItemTurnover', models.CharField(choices=[('S', 'Slow'), ('N', 'Normal'), ('F', 'Fast')], default='F', max_length=1)),
                ('siteItemMinThreshold', models.PositiveSmallIntegerField(default=0)),
                ('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='item.item')),
            ],
        ),
    ]

I moved my Inventory model to its own app and reset all migrations and the database. python manage.py makemigrations has no issues, but I get an error upon migrating. Hope someone could point me to a direction since the traceback doesn't seem very helpful to me for resolving the issue.

*I remove some parts of the code and traceback due to stack overflow limitations

Django version 4.0.2

traceback

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, inventory, item, project_site, requisition, sessions, transfer, users
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  ...
  Applying item.0001_initial... OK
  Applying project_site.0001_initial...Traceback (most recent call last):
  File "C:\Users\Bernard\pelicans\imrs-capstone\imrs\manage.py", line 22, in <module>
    main()
  File "C:\Users\Bernard\pelicans\imrs-capstone\imrs\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Bernard\pelicans\imrs-capstone\env\lib\site-packages\django\core\management\__init__.py", line 
425, in execute_from_command_line
    utility.execute()
...
  File "C:\Users\Bernard\pelicans\imrs-capstone\env\lib\site-packages\django\db\migrations\operations\models.py", line 92, in database_forwards
    schema_editor.create_model(model)
  File "C:\Users\Bernard\pelicans\imrs-capstone\env\lib\site-packages\django\db\backends\base\schema.py", line 364, in create_model
    if field.remote_field.through._meta.auto_created:
AttributeError: 'str' object has no attribute '_meta'

project_site/migrations/0001_initial.py

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('item', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Cart',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('cartItemCount', models.PositiveIntegerField(default=0)),
            ],
        ),
        migrations.CreateModel(
            name='Site',
            fields=[
                ...
                ('inventory_items', models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')),
                ('siteCart', models.ManyToManyField(blank=True, related_name='siteCart', through='project_site.Cart', to='item.Item')),
            ],
        ),
    ]

project_site/models.py

class Site(models.Model):
    siteCart = models.ManyToManyField(
        'item.Item',
        through='Cart',
        through_fields=('site', 'cartItem'),
        blank=True,
        related_name='siteCart')
    inventory_items = models.ManyToManyField(
        'item.Item',
        through='inventory.Inventory',
        through_fields=('site', 'item'),
        blank=True,
        related_name='inventory_items'
        )

inventory/models.py

class Inventory(models.Model):
    item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
    site = models.ForeignKey(Site, on_delete=models.CASCADE)
    ...

inventory/migrations/0001_initial.py

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('item', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Inventory',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('siteItemCount', models.PositiveIntegerField(default=0)),
                ('siteItemStatus', models.PositiveSmallIntegerField(choices=[(0, 'Above Threshold'), (1, 'Moderate'), (2, 'Low'), (3, 'Critical'), (4, 'Empty')], default=1)),
                ('siteItemTurnover', models.CharField(choices=[('S', 'Slow'), ('N', 'Normal'), ('F', 'Fast')], default='F', max_length=1)),
                ('siteItemMinThreshold', models.PositiveSmallIntegerField(default=0)),
                ('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='item.item')),
            ],
        ),
    ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2025-01-31 19:56:39

您有循环迁移依赖性的问题,project_site库存相互取决于彼此。如果您希望保持当前的应用程序/模型结构,则需要将迁移文件分解,以便可以在没有循环依赖项的步骤中应用它们。

首先删除库存中引用模型中的模型的 site 模型迁移

project_site_site/migrations/0001_initial.py:

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('item', '0001_initial'),
    ]

    operations = [
        ...
        migrations.CreateModel(
            name='Site',
            fields=[
                ...
                # delete this line ('inventory_items', models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')),
            ],
        ),
    ]

然后添加初始

> project_site 迁移作为初始库存迁移库存/迁移/0001_initial.py

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('item', '0001_initial'),
        ('project_site', '0001_initial'),
    ]

    ...

:然后在project_site中创建一个新的迁移添加> 应该可以使用这是可能的

Manytomanyfield ,现在

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('project_site', '0001_initial'),
        ('inventory', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='Site',
            name='inventory_items',
            field=models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')
        )
    ]

You have an issue with circular migration dependencies, both project_site and inventory depend on each other. If you wish to keep your current app/model structure you need to break your migration files up so that they can be applied in steps where there are no circular dependencies.

First remove the ManyToManyField that references the model in inventory from the Site model migration

project_site/migrations/0001_initial.py:

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('item', '0001_initial'),
    ]

    operations = [
        ...
        migrations.CreateModel(
            name='Site',
            fields=[
                ...
                # delete this line ('inventory_items', models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')),
            ],
        ),
    ]

Then add the initial project_site migration as a dependency in the initial inventory migration

inventory/migrations/0001_initial.py:

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('item', '0001_initial'),
        ('project_site', '0001_initial'),
    ]

    ...

Then create a new migration in project_site that adds the ManyToManyField, this should now be possible because the through model has already been created

project_site/migrations/0002_add_m2m.py:

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('project_site', '0001_initial'),
        ('inventory', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='Site',
            name='inventory_items',
            field=models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')
        )
    ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文