Django 1.10 default manager not MultilingualManager for model inheriting abstract class with a manager defined on objects (leading to AttributeError: 'QuerySet' object has no attribute 'rewrite')
See original GitHub issueWhilst upgrading my application to Django 1.10 I came across the following issue where the default manager for a Model Translation registered model didn’t get dynamically set to modeltranslation.manager.MultilingualManager
.
This resulted in an exception being thrown in update_translation_fields.py L32
AttributeError: 'QuerySet' object has no attribute 'rewrite'
A similar issue was resolved in #123 but it seems something in the internal changes in Django 1.10 has resurfaced it in the particular scenario I have (not one covered by #381).
I haven’t yet identified what the changes are that cause it but I’ve narrowed the scenario down to one in which a model inherits from an abstract model that explicitly sets a manager via the objects
attribute. In this case the resulting _default_manager
is that set on objects
rather than modeltranslation.manager.MultilingualManager
.
The simplest example I can boil it down to is this:
# models.py
class FooAbstract(models.Model):
class Meta:
abstract = True
class Foo(FooAbstract):
name = models.CharField(blank=True, max_length=1)
class Bar(models.Model):
objects = models.Manager()
name = models.CharField(blank=True, max_length=1)
class BazAbstract(models.Model):
objects = models.Manager()
class Meta:
abstract = True
class Baz(BazAbstract):
name = models.CharField(blank=True, max_length=1)
# translations.py
@register(Foo)
class FooTranslationOptions(TranslationOptions):
fields = ('name',)
@register(Bar)
class BarTranslationOptions(TranslationOptions):
fields = ('name',)
@register(Baz)
class BazTranslationOptions(TranslationOptions):
fields = ('name',)
# shell
from modeltranslation.translator import translator
models = translator.get_registered_models(abstract=False)
for model in models:
print(model.__name__, type(model._default_manager))
# Django 1.9 output:
Foo <class 'modeltranslation.manager.MultilingualManager'>
Bar <class 'modeltranslation.manager.MultilingualManager'>
Baz <class 'modeltranslation.manager.MultilingualManager'>
# Django 1.10 output:
Foo <class 'modeltranslation.manager.MultilingualManager'>
Bar <class 'modeltranslation.manager.MultilingualManager'>
Baz <class 'django.db.models.manager.Manager'>
I don’t have a solution yet but thought I’d submit it as an issue now in case anyone has any ideas?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:24
Top GitHub Comments
Now we have
django 1.11
and this bug isn’t solved yet.I need a moment of free time to take a deep look into the issue; probably will take care of it at the incoming weekend.