[Django] #24701: Migration created on python2.7 fail to load on python 3 due to models.Manager

25 views
Skip to first unread message

Django

unread,
Apr 24, 2015, 11:48:01 AM4/24/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fail to load on python 3 due to
models.Manager
----------------------------+--------------------
Reporter: aeby | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
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
[https://github.com/django/django/blob/1.8/django/db/models/manager.py#L169
this line].

The problem is that the string 'objects' is prefixed with 'b' on
[https://github.com/django/django/blob/1.8/django/db/migrations/writer.py#L340
this line].
I think this is maybe a bug as all other attribute names are just plain
strings.

--
Ticket URL: <https://code.djangoproject.com/ticket/24701>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 24, 2015, 11:49:48 AM4/24/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fail to load on python 3 due to
models.Manager
----------------------------+--------------------------------------

Reporter: aeby | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by MarkusH):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:1>

Django

unread,
Apr 24, 2015, 12:03:45 PM4/24/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fail to load on python 3 due to
models.Manager
----------------------------+--------------------------------------

Reporter: aeby | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------

Comment (by aeby):

=== On python 2.7 ===

''models.py''
{{{#!python
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''
{{{#!python
# -*- 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''
{{{#!bash
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'
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:2>

Django

unread,
Apr 24, 2015, 12:14:15 PM4/24/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fail to load on python 3 due to
models.Manager
----------------------------+------------------------------------

Reporter: aeby | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by charettes):

* stage: Unreviewed => Accepted


Comment:

It looks like we'll need special casing of manager attribute name here
[https://github.com/django/django/blob/8efea1b8d5b5fb0cfef1a3244c339cebf8af36c5/django/db/models/fields/__init__.py#L446
just like we do with model fields].

--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:3>

Django

unread,
Apr 24, 2015, 12:17:50 PM4/24/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fail to load on python 3 due to
models.Manager
----------------------------+------------------------------------
Reporter: aeby | Owner: MarkusH
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by MarkusH):

* owner: nobody => MarkusH
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:4>

Django

unread,
Apr 24, 2015, 5:42:15 PM4/24/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fails to load on python 3 due to
models.Manager

----------------------------+------------------------------------
Reporter: aeby | Owner: MarkusH
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:5>

Django

unread,
Apr 24, 2015, 9:10:34 PM4/24/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fails to load on python 3 due to
models.Manager

----------------------------+------------------------------------
Reporter: aeby | Owner: MarkusH
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by MarkusH):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/4558

--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:6>

Django

unread,
Apr 25, 2015, 12:15:03 PM4/25/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fails to load on python 3 due to
models.Manager
----------------------------+---------------------------------------------

Reporter: aeby | Owner: MarkusH
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:7>

Django

unread,
Apr 25, 2015, 12:18:09 PM4/25/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fails to load on python 3 due to
models.Manager

----------------------------+---------------------------------------------
Reporter: aeby | Owner: MarkusH
Type: Bug | Status: closed
Component: Migrations | Version: 1.8
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------
Changes (by Markus Holtermann <info@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"faad6070ee2eeea270c76381f9ca5999bf1ac15f" faad607]:
{{{
#!CommitTicketReference repository=""
revision="faad6070ee2eeea270c76381f9ca5999bf1ac15f"
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.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:8>

Django

unread,
Apr 25, 2015, 12:20:14 PM4/25/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fails to load on python 3 due to
models.Manager

----------------------------+---------------------------------------------
Reporter: aeby | Owner: MarkusH
Type: Bug | Status: closed
Component: Migrations | Version: 1.8

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------

Comment (by Markus Holtermann <info@…>):

In [changeset:"419f296259d7c22d34d7d1cf46e7cf29e6b26b13" 419f2962]:
{{{
#!CommitTicketReference repository=""
revision="419f296259d7c22d34d7d1cf46e7cf29e6b26b13"
[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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:9>

Django

unread,
Apr 25, 2015, 7:42:12 PM4/25/15
to django-...@googlegroups.com
#24701: Migration created on python2.7 fails to load on python 3 due to
models.Manager

----------------------------+---------------------------------------------
Reporter: aeby | Owner: MarkusH
Type: Bug | Status: closed
Component: Migrations | Version: 1.8

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+---------------------------------------------

Comment (by aeby):

Wow, you are fast. Thanks & respect!

--
Ticket URL: <https://code.djangoproject.com/ticket/24701#comment:10>

Reply all
Reply to author
Forward
0 new messages