[Django] #20977: Encoding error writing new migrations on Python 3

6 views
Skip to first unread message

Django

unread,
Aug 26, 2013, 8:42:58 AM8/26/13
to django-...@googlegroups.com
#20977: Encoding error writing new migrations on Python 3
----------------------------+--------------------
Reporter: MarkusH | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
When trying to create a migration using Python 3 I get the following
error:

{{{
$ python manage.py makemigrations something -v3
Migrations for 'something':
0001_initial.py:
- Create model MyModel
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/markus/.venvs/django-master-
py3/src/django/django/core/management/__init__.py", line 397, in
execute_from_command_line
utility.execute()
File "/home/markus/.venvs/django-master-
py3/src/django/django/core/management/__init__.py", line 390, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/markus/.venvs/django-master-
py3/src/django/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/markus/.venvs/django-master-
py3/src/django/django/core/management/base.py", line 289, in execute
output = self.handle(*args, **options)
File "/home/markus/.venvs/django-master-
py3/src/django/django/core/management/commands/makemigrations.py", line
86, in handle
fh.write(writer.as_string())
TypeError: must be str, not bytes
}}}

I got this fixed by a small change:

{{{
diff --git a/django/core/management/commands/makemigrations.py
b/django/core/management/commands/makemigrations.py
index d802e29..a0befaa 100644
--- a/django/core/management/commands/makemigrations.py
+++ b/django/core/management/commands/makemigrations.py
@@ -80,5 +80,5 @@ class Command(BaseCommand):
open(init_path, "w").close()
# We just do this once per app
directory_created[app_label] = True
- with open(writer.path, "w") as fh:
+ with open(writer.path, "wb") as fh:
fh.write(writer.as_string())
}}}

However, given this models.py, I get different migration files depending
of the Python version I use to create the migration:

{{{
# -*- coding: utf-8 -*-
from django.db import models
from django.utils.encoding import python_2_unicode_compatible


@python_2_unicode_compatible
class MyModel(models.Model):
name = models.CharField('Name', max_length=50, unique=True)
text = models.TextField('Text', blank=True, null=True)

class Meta:
verbose_name = 'My “fancy” Model'
verbose_name_plural = 'My “fancy” Models'

def __str__(self):
return self.name
}}}

The migration file for Python 3:

{{{
# encoding: utf8
from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = []

operations = [
migrations.CreateModel(
options = {'verbose_name_plural': 'My “fancy” Models',
'verbose_name': 'My “fancy” Model'},
bases = (models.Model,),
name = 'MyModel',
fields = [('id', models.AutoField(primary_key=True,
auto_created=True, serialize=False, verbose_name='ID'),), ('name',
models.CharField(max_length=50, verbose_name='Name', unique=True),),
('text', models.TextField(blank=True, verbose_name='Text', null=True),)],
),
]
}}}

The migration file for Python 2:

{{{
# encoding: utf8
from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = []

operations = [
migrations.CreateModel(
fields = [(u'id', models.AutoField(verbose_name=u'ID',
serialize=False, auto_created=True, primary_key=True),), ('name',
models.CharField(unique=True, max_length=50, verbose_name='Name'),),
('text', models.TextField(null=True, verbose_name='Text', blank=True),)],
bases = (models.Model,),
options = {u'verbose_name': 'My \xe2\x80\x9cfancy\xe2\x80\x9d
Model', u'verbose_name_plural': 'My \xe2\x80\x9cfancy\xe2\x80\x9d
Models'},
name = 'MyModel',
),
]
}}}

Note the differences for the "options":
{{{
options = {'verbose_name_plural': 'My “fancy” Models', 'verbose_name': 'My
“fancy” Model'},
}}}
vs
{{{
options = {u'verbose_name': 'My \xe2\x80\x9cfancy\xe2\x80\x9d Model',
u'verbose_name_plural': 'My \xe2\x80\x9cfancy\xe2\x80\x9d Models'},
}}}

As far as I have tested, this does not make a problem when using the other
Python version to apply the migration.

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

Django

unread,
Aug 27, 2013, 9:09:35 AM8/27/13
to django-...@googlegroups.com
#20977: Encoding error writing new migrations on Python 3
----------------------------+------------------------------------

Reporter: MarkusH | Owner:
Type: Bug | Status: new
Component: Migrations | Version: master
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 timo):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


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

Django

unread,
Sep 1, 2013, 11:28:58 AM9/1/13
to django-...@googlegroups.com
#20977: Encoding error writing new migrations on Python 3
----------------------------+------------------------------------
Reporter: MarkusH | Owner: MarkusH
Type: Bug | Status: assigned
Component: Migrations | Version: master

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

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

* owner: => MarkusH
* needs_docs: 0 => 1
* status: new => assigned


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

Django

unread,
Sep 1, 2013, 11:49:16 AM9/1/13
to django-...@googlegroups.com
#20977: Encoding error writing new migrations on Python 3
----------------------------+------------------------------------
Reporter: MarkusH | Owner: MarkusH
Type: Bug | Status: assigned
Component: Migrations | Version: master

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

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

* cc: info@… (added)


Comment:

I opened a pull request at https://github.com/django/django/pull/1540

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

Django

unread,
Sep 6, 2013, 9:52:52 AM9/6/13
to django-...@googlegroups.com
#20977: Encoding error writing new migrations on Python 3
----------------------------+------------------------------------
Reporter: MarkusH | Owner: MarkusH
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: fixed

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

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by Tim Graham <timograham@…>):

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


Comment:

In [changeset:"bd8e1a354cb1fde5e5411e3c729a0d179f4eb37b"]:
{{{
#!CommitTicketReference repository=""
revision="bd8e1a354cb1fde5e5411e3c729a0d179f4eb37b"
Fixed #20977 -- Fixed writing migrations to disk on Python 3
}}}

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

Reply all
Reply to author
Forward
0 new messages