#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 , 10 years ago
comment:2 by , 10 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 , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|
It looks like we'll need special casing of manager attribute name here just like we do with model fields.
comment:4 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 10 years ago
Summary: | Migration created on python2.7 fail to load on python 3 due to models.Manager → Migration created on python2.7 fails to load on python 3 due to models.Manager |
---|
comment:7 by , 10 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Can you please post the entire traceback and the code you are using to define the model and manager.