[Django] #33019: makemigrations produces invalid import path in migration file

2 views
Skip to first unread message

Django

unread,
Aug 12, 2021, 11:50:22 PM8/12/21
to django-...@googlegroups.com
#33019: makemigrations produces invalid import path in migration file
--------------------------------------+--------------------------------
Reporter: zepx | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 3.2
Severity: Normal | Keywords: makemigrations
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+--------------------------------
This issue is somewhat similar to
https://code.djangoproject.com/ticket/27914 with a slightly different
problem.

If you define an inner class in the `django.db.models.Model` subclass,
`makemigrations` will produce an invalid import path resulting in import
error.

Specifically, if we have the following structure in our `models.py`,

{{{#!python
from django.db import models


class OuterClass(models.Model):
class InnerField(models.CharField):
pass

outer_field = InnerField(max_length=100)
}}}

the generated migration file would produce

{{{#!python
# Generated by Django 3.2.6 on 2021-08-13 03:39

from django.db import migrations, models
import test1.models.OuterClass


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='OuterClass',
fields=[
('id', models.BigAutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('outer_field',
test1.models.OuterClass.InnerField(max_length=100)),
],
),
]
}}}

Notice that the import path at the top refers to an import of a class.
This is an invalid import format.

{{{#!python
import test1.models.OuterClass
}}}

Further usage of `manage.py migrate` will result in errors:

{{{#!python
ModuleNotFoundError: No module named 'test1.models.OuterClass';
'test1.models' is not a package
}}}

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

Django

unread,
Aug 12, 2021, 11:51:56 PM8/12/21
to django-...@googlegroups.com
#33019: makemigrations produces invalid import path in migration file
--------------------------------+--------------------------------------

Reporter: zepx | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 3.2
Severity: Normal | Resolution:

Keywords: makemigrations | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Description changed by zepx:

Old description:

New description:

outer_field = InnerField(max_length=100)
}}}


class Migration(migrations.Migration):

initial = True

dependencies = [
]

{{{#!python
import test1.models.OuterClass
}}}

If we change the import to

{{{#!python
import test1.models
}}}

the migration file will work accordingly.

--

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

Django

unread,
Aug 17, 2021, 3:11:56 AM8/17/21
to django-...@googlegroups.com
#33019: makemigrations produces invalid import path in migration file
--------------------------------+--------------------------------------
Reporter: Choong Jun Jin | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: makemigrations | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => invalid


Comment:

You'll need to [https://docs.djangoproject.com/en/3.2/topics/migrations
/#custom-serializers register a custom serializer] that knows how to
handle your nested field. (A nice example is
[https://github.com/django/django/blob/6c3525a09db5177bf4e3856de85bf8b1300402d5/django/db/migrations/serializer.py#L62-L70
`DatetimeDatetimeSerializer` which adjusts to the `tzinfo` to
`django.utils.timezone.utc`, rather than the underlying implmentation].)

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

Reply all
Reply to author
Forward
0 new messages