Opened 3 years ago
Closed 3 years ago
#33019 closed Bug (invalid)
makemigrations produces invalid import path in migration file
Reported by: | Choong Jun Jin | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 3.2 |
Severity: | Normal | Keywords: | makemigrations |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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
,
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
# 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.
import test1.models.OuterClass
Further usage of manage.py migrate
will result in errors:
ModuleNotFoundError: No module named 'test1.models.OuterClass'; 'test1.models' is not a package
If we change the import to
import test1.models
the migration file will work accordingly.
Change History (2)
comment:1 by , 3 years ago
Description: | modified (diff) |
---|
comment:2 by , 3 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
You'll need to register a custom serializer that knows how to handle your nested field. (A nice example is `DatetimeDatetimeSerializer` which adjusts to the `tzinfo` to `django.utils.timezone.utc`, rather than the underlying implmentation.)