gpt4 book ai didi

python - Django 类型错误 : 'ModelBase' object is not iterable

转载 作者:行者123 更新时间:2023-11-28 21:09:42 29 4
gpt4 key购买 nike

在执行任何迁移命令时,我都在努力调试 Django 中的模糊错误消息:

Operations to perform:
Synchronize unmigrated apps: rest_framework_docs, staticfiles, django_coverage, django_extensions, storages, corsheaders, gis, templated_email, rest_framework, django_mptt_admin, opbeat.contrib.django, grappelli, permissions, django_nose, django_markdown, messages, common
Apply all migrations: contenttypes, auth, badges, reputation, geodata, comments, sites, users, votes, watchers, library, sessions, admin, oauth2_provider
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
No migrations to apply.

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/user/Documents/workspace/app-api/env/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
utility.execute()
File "/Users/user/Documents/workspace/app-api/env/lib/python3.4/site-packages/django/core/management/__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/user/Documents/workspace/app-api/env/lib/python3.4/site-packages/django/core/management/base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/user/Documents/workspace/app-api/env/lib/python3.4/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/Users/user/Documents/workspace/app-api/env/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 205, in handle
executor.loader.project_state(),
File "/Users/user/Documents/workspace/app-api/env/lib/python3.4/site-packages/django/db/migrations/loader.py", line 353, in project_state
return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
File "/Users/user/Documents/workspace/app-api/env/lib/python3.4/site-packages/django/db/migrations/graph.py", line 231, in make_state
project_state = self.nodes[node].mutate_state(project_state, preserve=False)
File "/Users/user/Documents/workspace/app-api/env/lib/python3.4/site-packages/django/db/migrations/migration.py", line 83, in mutate_state
operation.state_forwards(self.app_label, new_state)
File "/Users/user/Documents/workspace/user-api/env/lib/python3.4/site-packages/django/db/migrations/operations/models.py", line 52, in state_forwards
tuple(self.bases),
TypeError: 'ModelBase' object is not iterable

我已将问题追溯到我的一个应用程序,将其删除即可清除错误。但是,上面的错误并不能帮助我理解这个问题。 Runserver 工作正常,没有错误输出。

Django 中的这一行产生错误:https://github.com/django/django/blob/master/django/db/migrations/operations/models.py#L83

这个错误与什么有关?应用程序本身不会产生任何错误并且运行良好,出现此错误的应用程序是 reputation

环境:

pillow==3.2.0
Django==1.8.7
psycopg2
Sphinx==1.2.3
django-mptt==0.7.2
sphinx_rtd_theme==0.1.6
django_extensions==1.3.3
django-mptt-admin==0.2.1
django-debug-toolbar==1.4
djangorestframework==3.3.3
django-oauth-toolkit==0.8.1
rest_condition==1.0.1
gevent==1.1rc3
gunicorn==19.3.0
django-cors-headers==1.1.0
django-simple-email-confirmation==0.12
itsdangerous==0.24
django-grappelli==2.7.1
numpy==1.11.0

声誉迁移:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals



class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('contenttypes', '0002_remove_content_type_name'),
]

operations = [
migrations.CreateModel(
name='Reputation',
fields=[
('created', django_extensions.db.fields.CreationDateTimeField(default=django.utils.timezone.now, blank=True, editable=False, verbose_name='created')),
('modified', django_extensions.db.fields.ModificationDateTimeField(default=django.utils.timezone.now, blank=True, editable=False, verbose_name='modified')),
('id', models.UUIDField(primary_key=True, unique=True,
('reputation', models.PositiveIntegerField(default=0)),
('dimension', models.CharField(max_length=2, blank=True, null=True)),
('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='reputation_set')),
],
options={
'verbose_name': 'Reputation',
},
),
migrations.CreateModel(
name='ReputationAction',
fields=[
('created', django_extensions.db.fields.CreationDateTimeField(default=django.utils.timezone.now, blank=True, editable=False, verbose_name='created')),
('modified', django_extensions.db.fields.ModificationDateTimeField(default=django.utils.timezone.now, blank=True, editable=False, verbose_name='modified')),
('id', models.UUIDField(primary_key=True, unique=True,
('action_type', models.CharField(max_length=10, blank=True, null=True)),
('value', models.IntegerField(default=0)),
('capped', models.BooleanField(default=0)),
('object_id', models.UUIDField()),
('content_type', models.ForeignKey(to='contenttypes.ContentType')),
('originating_user', models.ForeignKey(related_name='originating_user', to=settings.AUTH_USER_MODEL, null=True)),
('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='target_user')),
],
options={
'verbose_name': 'Reputation Action',
},
bases=(models.Model),
),
]

2:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='reputationaction',
name='user',
field=models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='reputation_action'),
),
]

3

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


def dedupe_reputation_actions(apps, schema_editor):
Reputation = apps.get_model('reputation', 'Reputation')

qs = Reputation.objects.all().values('user', 'dimension')\
.annotate(total=models.Count('user'))\
.filter(total__gt=1)

for dupe in qs:
reps = list(Reputation.objects.filter(user=dupe['user'], dimension=dupe['dimension']))

for rep in reps[1:]:
rep.delete()


class Migration(migrations.Migration):

dependencies = [
('reputation', '0002_auto_20151117_1108'),
]

operations = [
migrations.RunPython(dedupe_reputation_actions)
]

4

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('reputation', '0003_auto_20160302_1749'),
]

operations = [
migrations.AlterUniqueTogether(
name='reputation',
unique_together=set([('user', 'dimension')]),
),
]

最佳答案

在您的第一次迁移中:

migrations.CreateModel(
name='ReputationAction',
...
bases=(models.Model),
),

您需要将 bases=(models.Model), 更改为 bases=(models.Model,),(注意多余的逗号)。这将使 bases 成为包含基础 Model 类的元组,而不仅仅是模型类。

我不确定您的迁移将如何进入这种状态。您是否手动更改了迁移?您可能想将 Django 升级到最新的 1.8.x 版本,也许有一个错误已被修复。

关于python - Django 类型错误 : 'ModelBase' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37846721/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com