Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#24701 closed Bug (fixed)

Migration created on python2.7 fails to load on python 3 due to models.Manager

Reported by: Reto Aebersold Owned by: Markus Holtermann
Component: Migrations Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I create a migration on python 2.7 containing a model referencing a model.Manager with use_in_migrations = True the following line is added in the migration file:

managers=[
                (b'objects', accounts.models.UserManager()),
            ],

If I try to load this file under python 3.4 I get the error TypeError: attribute name must be string, not 'bytes from this line.

The problem is that the string 'objects' is prefixed with 'b' on this line.
I think this is maybe a bug as all other attribute names are just plain strings.

Change History (10)

comment:1 by Markus Holtermann, 9 years ago

Can you please post the entire traceback and the code you are using to define the model and manager.

comment:2 by Reto Aebersold, 9 years ago

On python 2.7

models.py

from django.db import models


class MyManager(models.Manager):
    use_in_migrations = True

    def something(self):
        print('demo manager')


class MyModel(models.Model):
    demo = models.BooleanField(default=True)

    objects = MyManager()

0001_initial.py

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

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


class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='MyModel',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('demo', models.BooleanField(default=True)),
            ],
            managers=[
                (b'objects', demo.models.MyManager()),
            ],
        ),
    ]

On python 3.4

$ ./manage.py migrate demo

Operations to perform:
  Apply all migrations: demo
Running migrations:
  Rendering model states...Traceback (most recent call last):
  File "./manage.py", line 35, in <module>
    execute_from_command_line(sys.argv)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 221, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/migrations/executor.py", line 104, in migrate
    state = migration.mutate_state(state, preserve=do_run)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/migrations/migration.py", line 83, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/migrations/operations/models.py", line 53, in state_forwards
    list(self.managers),
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/migrations/state.py", line 81, in add_model
    self.reload_model(app_label, model_name)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/migrations/state.py", line 140, in reload_model
    self.apps.render_multiple(states_to_be_rendered)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/migrations/state.py", line 250, in render_multiple
    model.render(self)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/migrations/state.py", line 533, in render
    body,
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/models/base.py", line 189, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/aeby/code/myproject/venv/lib/python3.4/site-packages/django/db/models/manager.py", line 169, in contribute_to_class
    setattr(model, name, ManagerDescriptor(self))
TypeError: attribute name must be string, not 'bytes'

comment:3 by Simon Charette, 9 years ago

Triage Stage: UnreviewedAccepted

It looks like we'll need special casing of manager attribute name here just like we do with model fields.

comment:4 by Markus Holtermann, 9 years ago

Owner: changed from nobody to Markus Holtermann
Status: newassigned

comment:5 by Reto Aebersold, 9 years ago

Summary: Migration created on python2.7 fail to load on python 3 due to models.ManagerMigration created on python2.7 fails to load on python 3 due to models.Manager

comment:6 by Markus Holtermann, 9 years ago

Has patch: set

comment:7 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Markus Holtermann <info@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In faad607:

Fixed #24701 -- Converted model manager names to unicode in migrations

Thanks to Reto Aebersold for reporting the issue and Tim Graham and
Claude Paroz for the review.

comment:9 by Markus Holtermann <info@…>, 9 years ago

In 419f2962:

[1.8.x] Fixed #24701 -- Converted model manager names to unicode in migrations

Thanks to Reto Aebersold for reporting the issue and Tim Graham and
Claude Paroz for the review.

Backport of faad6070ee2eeea270c76381f9ca5999bf1ac15f from master

comment:10 by Reto Aebersold, 9 years ago

Wow, you are fast. Thanks & respect!

Note: See TracTickets for help on using tickets.
Back to Top