AttributeError在迁移中调用自定义管理器方法

时间:2017-04-05 02:54:28

标签: python django migration django-custom-manager

我正在使用Django 1.10.3,在迁移步骤中,当我使用RunPython()调用自定义管理器方法时遇到错误。我有什么不正确的想法吗?

错误消息是:AttributeError:'Manager'对象没有属性'current_event'

我的模特和经理:

class EventManager(models.Manager):
    use_in_migrations = True

    def current_event(self):
        try:
            the_event = self.filter(
                            event_date__gte=date.today()
                        ).earliest(
                            field_name='event_date'
                        )
        except ObjectDoesNotExist:
            the_event = None

        return the_event

class Event(models.Model):
    event_date = models.DateField()
    objects = EventManager()

我的迁移:

def update_ratings_event(apps, schema_editor):
    Rating = apps.get_model('league', 'Rating')
    Event = apps.get_model('league', 'Event')

    recent_event = Event.objects.current_event()

    for a_rating in Rating.objects.all():
        a_rating.event = recent_event
        a_rating.save()

class Migration(migrations.Migration):

    dependencies = [
        ('league', '0009_auto_20170401_1106'),
    ]

    operations = [
        migrations.RunPython(update_ratings_event),
    ]

这是追溯:

  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/mriley/.virtualenvs/League/lib/python3.5/site-packages/django/db/migrations/operations/special.py", line 189, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/home/mriley/Development/League/website/league/migrations/0010_auto_20170404_1645.py", line 12, in update_ratings_event
    recent_event = Event.objects.current_event()
AttributeError: 'Manager' object has no attribute 'current_event'

0 个答案:

没有答案