#26605: Abstract model inheriting concrete model crashes migrations
----------------------------+------------------------------------
Reporter: sebdiem | Owner: akki
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Description changed by sebdiem:
Old description:
> When using Model inheritance with an abstract model in between, it can
> happen that the _default_manager of the actual child class is None. In
> this case the //ModelState.from_model// function fails with the following
> error:
> {{{
> django/django/db/migrations/state.py", line 439, in from_model
> default_manager_name = force_text(model._default_manager.name)
> AttributeError: 'NoneType' object has no attribute 'name'
> }}}
>
> The following modified test reproduces the error:
> {{{
> @override_settings(TEST_SWAPPABLE_MODEL='migrations.SomeFakeModel')
> def test_create_swappable(self):
> """
> Tests making a ProjectState from an Apps with a swappable model
> """
> new_apps = Apps(['migrations'])
>
> class Base(models.Model):
> pass
>
> class AbstractAuthor(Base):
> class Meta:
> abstract = True
>
> class Author(AbstractAuthor):
> name = models.CharField(max_length=255)
> bio = models.TextField()
> age = models.IntegerField(blank=True, null=True)
>
> class Meta(AbstractAuthor.Meta):
> app_label = 'migrations'
> apps = new_apps
> swappable = 'TEST_SWAPPABLE_MODEL'
>
> author_state = ModelState.from_model(Author)
> self.assertEqual(author_state.app_label, 'migrations')
> self.assertEqual(author_state.name, 'Author')
> self.assertEqual([x for x, y in author_state.fields], ['id',
> 'name', 'bio', 'age'])
> self.assertEqual(author_state.fields[1][1].max_length, 255)
> self.assertEqual(author_state.fields[2][1].null, False)
> self.assertEqual(author_state.fields[3][1].null, True)
> self.assertEqual(author_state.options, {'abstract': False,
> 'swappable': 'TEST_SWAPPABLE_MODEL'})
> self.assertEqual(author_state.bases, (models.Model, ))
> self.assertEqual(author_state.managers, [])
> }}}
>
> Changing the following line in //ModelState.from_model//
> {{{
> if hasattr(model, "_default_manager"):
> }}}
> to
> {{{
> if getattr(model, "_default_manager", None):
> }}}
> is probably sufficient to fix the bug.
New description:
When using Model inheritance with an abstract model in between, it can
happen that the _default_manager of the actual child class is None. In
this case the //ModelState.from_model// function fails with the following
error:
{{{
django/django/db/migrations/state.py", line 439, in from_model
default_manager_name = force_text(model._default_manager.name)
AttributeError: 'NoneType' object has no attribute 'name'
}}}
The following modified test reproduces the error:
{{{
@override_settings(TEST_SWAPPABLE_MODEL='migrations.SomeFakeModel')
def test_create_swappable(self):
"""
Tests making a ProjectState from an Apps with a swappable model
"""
new_apps = Apps(['migrations'])
class Base(models.Model):
pass
class AbstractAuthor(Base):
class Meta:
abstract = True
class Author(AbstractAuthor):
name = models.CharField(max_length=255)
bio = models.TextField()
age = models.IntegerField(blank=True, null=True)
class Meta(AbstractAuthor.Meta):
app_label = 'migrations'
apps = new_apps
swappable = 'TEST_SWAPPABLE_MODEL'
author_state = ModelState.from_model(Author)
self.assertEqual(author_state.app_label, 'migrations')
self.assertEqual(author_state.name, 'Author')
self.assertEqual([x for x, y in author_state.fields], ['id',
'name', 'bio', 'age'])
self.assertEqual(author_state.fields[1][1].max_length, 255)
self.assertEqual(author_state.fields[2][1].null, False)
self.assertEqual(author_state.fields[3][1].null, True)
self.assertEqual(author_state.options, {'abstract': False,
'swappable': 'TEST_SWAPPABLE_MODEL'})
self.assertEqual(author_state.bases, (models.Model, ))
self.assertEqual(author_state.managers, [])
}}}
Changing the following line in //ModelState.from_model//
{{{
if hasattr(model, "_default_manager"):
}}}
to
{{{
if getattr(model, "_default_manager", None):
}}}
is probably sufficient to fix the bug.
See also PR https://github.com/django/django/pull/6847
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26605#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.fd949aaf10c505e93c6c6f7e7bbd9391%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.