简体   繁体   English

Django TypeError:“ ModelBase”对象不可迭代

[英]Django TypeError: 'ModelBase' object is not iterable

I'm struggling to debug a vague error message in Django when executing any migration command: 执行任何迁移命令时,我都难以在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

I've tracked the problem down to one of my apps, removing it clears the error. 我已将问题归结为我的一个应用程序,将其删除可以清除错误。 However, the error above does not help me understand the issue. 但是,上面的错误并不能帮助我理解问题。 Runserver works fine with no errors output. Runserver运行正常,没有错误输出。

The error is being generated from this line in Django: https://github.com/django/django/blob/master/django/db/migrations/operations/models.py#L83 错误是从Django的以下行生成的: https : //github.com/django/django/blob/master/django/db/migrations/operations/models.py#L83

What does this error relate to? 此错误与什么有关? The app itself does not generate any errors and runs fine, the app which gives this error is reputation . 该应用程序本身不会产生任何错误并且可以正常运行,给出此错误的应用程序是reputation

Environment: 环境:

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

reputation migrations: 名誉迁移:

# -*- 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: 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 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 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')]),
        ),
    ]

In your first migration: 在您的第一次迁移中:

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

You need to change bases=(models.Model), to bases=(models.Model,), (note the extra comma). 您需要将bases=(models.Model),更改为bases=(models.Model,), (请注意额外的逗号)。 This will make bases a tuple containing the base Model class, rather than just the model class. 这将使bases元成为包含基类Model的元组,而不仅仅是基元类。

I'm not sure how your migrations would get into this state. 我不确定您的迁移将如何进入这种状态。 Did you change the migrations manually? 您是否手动更改了迁移? You might want to upgrade Django to the latest 1.8.x version, maybe there was a bug that has since been fixed. 您可能想将Django升级到最新的1.8.x版本,也许此后已修复了一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM