-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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') #389
Comments
Which version of MT are you using? |
Sorry, I forgot to add that info! It's the latest 0.12 from PyPi. |
Python 3.5.2 We are told here django/django@ed0ff91 that:
On the following file - https://github.com/zlorf/django-modeltranslation/blob/master/modeltranslation/manager.py#L141 - we can see this: class MultilingualManager(models.Manager):
use_for_related_fields = True
def get_query_set(self):
return MultilingualQuerySet(self.model) This snippet seems to abide by the "Old API" instead of the new one. Might be related to this problem? |
@Ge0 I did wonder whether that and inheritance changes documented in the 1.10 release notes were the source of the change, but they are deprecations in 1.10 so the pre-1.10 behaviour should still remain:
|
The 1.10 release notes don't mention it, but manager inheritance rules have changed between 1.9 and 1.10 which is likely to affect the chosen default manager: Compare Django 1.10 Custom managers and model inheritance with Django 1.9 Custom managers and model inheritance |
Maybe we have to take a look at the https://github.com/deschler/django-modeltranslation/blob/master/modeltranslation/translator.py#L423 I am sorry if this comment is useless. I try to find leads to fix this issue and my poor python knowledge helps me from getting things done quickly at the moment. |
I might have found another lead here: Since the version 1.10, the Therefore I think of adding a What do you guys think? |
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. |
Is there any way around this while this gets fixed ? |
A workaround that seems to work for me (without detailed testing) is to redeclare E.g., using my examples from the first comment of this issue:
(I'm redeclaring Fingers crossed for a fix, Model Translation is so useful! |
A caveat to my previous comment about a workaround. This only applies to the default manager on the subclass. Other managers inherited from the abstract base class won't be patched to become subclasses of MultilingualManager, so take care if using them with translation-registered fields because they won't work as expected (they'll just use the original non-language-suffixed database field). |
is there any update on this? @zlorf have you started working on this issue yet? |
Looking at how the new |
Workaround: I've monkey-patched Mezzanine like below and two models started working. Of course got stuck on another that didn't use Displayable, but fix should be identical. --- /usr/local/lib/python3.5/site-packages/mezzanine/core/models.py.orig 2016-09-26 01:49:47.000000000 +0200
+++ /usr/local/lib/python3.5/site-packages/mezzanine/core/models.py 2017-01-26 23:08:47.244624046 +0100
@@ -216,6 +216,15 @@
SHORT_URL_UNSET = "unset"
+def wrapped_manager(klass):
+ if settings.USE_MODELTRANSLATION:
+ from modeltranslation.manager import MultilingualManager
+ class Mgr(MultilingualManager, klass):
+ pass
+ return Mgr()
+ else:
+ return klass()
+
class Displayable(Slugged, MetaData, TimeStamped):
"""
Abstract model that provides features of a visible page on the
@@ -236,7 +245,7 @@
short_url = models.URLField(blank=True, null=True)
in_sitemap = models.BooleanField(_("Show in sitemap"), default=True)
- objects = DisplayableManager()
+ objects = wrapped_manager(DisplayableManager)
search_fields = {"keywords": 10, "title": 5}
class Meta: |
Full fix for Mezzanine 4.2.2 for reference. |
I am using Zinnia and fix above didn't work for me so I figured out another workaround:
I extracted code above directly from modeltranslation/translator.py and used it to reapply the modeltranslation manager patch to original managers from the abstract class. Anyway debugging modeltranslation/translator.py code
I noticed:
Moving away code related to point 3) (before |
Now we have |
I think that documenting the add of the manager used in the subclass could be a good workaround |
I am having this issue in models that combine django-polymorphic with modeltranslation. The objects redefinition workaround fixed update_translation_fields; I am checking if the admin needs more work. |
With several level of inheritance, custom managers with custom querysets, and a ManyToMany field with an explicit intermediary table. This reproduces bugs deschler#389 and deschler#413.
@Schnouki, @zlorf, @deschler, PR #431 fixed the issue for me. While using
I believe this is due to my After installing modeltranslation from @Schnouki's branch the command worked flawlessly. |
Please tell us when this is released! :) |
Does Django 2.x have with this bug? |
That was probably not related to django, and by now this bug must be fixed. |
Whilst 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 onobjects
rather thanmodeltranslation.manager.MultilingualManager
.The simplest example I can boil it down to is this:
I don't have a solution yet but thought I'd submit it as an issue now in case anyone has any ideas?
The text was updated successfully, but these errors were encountered: