attributeError:' str'对象没有属性' _Meta'迁移
我将库存模型移至自己的应用程序中,并重置所有迁移和数据库。 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有循环迁移依赖性的问题,
project_site
和库存
相互取决于彼此。如果您希望保持当前的应用程序/模型结构,则需要将迁移文件分解,以便可以在没有循环依赖项的步骤中应用它们。首先删除
库存中引用模型
中的模型的site 模型迁移
project_site_site/migrations/0001_initial.py:
然后添加初始
> project_site 迁移作为初始
库存
迁移库存/迁移/0001_initial.py:然后在
project_site
中创建一个新的迁移添加> 应该可以使用这是可能的
Manytomanyfield ,现在
You have an issue with circular migration dependencies, both
project_site
andinventory
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 ininventory
from theSite
model migrationproject_site/migrations/0001_initial.py:
Then add the initial
project_site
migration as a dependency in the initialinventory
migrationinventory/migrations/0001_initial.py:
Then create a new migration in
project_site
that adds theManyToManyField
, this should now be possible because the through model has already been createdproject_site/migrations/0002_add_m2m.py: