Skip to content

Instantly share code, notes, and snippets.

@melvyn-sopacua
Created January 26, 2017 22:44
Show Gist options
  • Save melvyn-sopacua/1f8dd8d1ed4b632ea09252b0e3fa01b2 to your computer and use it in GitHub Desktop.
Save melvyn-sopacua/1f8dd8d1ed4b632ea09252b0e3fa01b2 to your computer and use it in GitHub Desktop.
Fix Mezzanine 4.2.2 on Django 1.10+ with modeltranslation
--- ./pages/models.py.orig 2016-09-08 03:02:33.000000000 +0200
+++ ./pages/models.py 2017-01-26 23:26:16.672548649 +0100
@@ -18,6 +18,7 @@
from mezzanine.pages.fields import MenusField
from mezzanine.pages.managers import PageManager
from mezzanine.utils.urls import path_to_slug
+from mezzanine.core.models import wrapped_manager
class BasePage(Orderable, Displayable):
@@ -27,7 +28,7 @@
``Page`` subclass loses the custom manager.
"""
- objects = PageManager()
+ objects = wrapped_manager(PageManager)
class Meta:
abstract = True
--- ./core/models.py.orig 2016-09-26 01:49:47.000000000 +0200
+++ ./core/models.py 2017-01-26 23:31:14.478528878 +0100
@@ -32,6 +32,15 @@
user_model_name = get_user_model_name()
+def wrapped_manager(klass):
+ if settings.USE_MODELTRANSLATION:
+ from modeltranslation.manager import MultilingualManager
+ class Mgr(MultilingualManager, klass):
+ pass
+ return Mgr()
+ else:
+ return klass()
+
class SiteRelated(models.Model):
"""
@@ -41,7 +50,7 @@
details.
"""
- objects = CurrentSiteManager()
+ objects = wrapped_manager(CurrentSiteManager)
class Meta:
abstract = True
@@ -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:
--- ./forms/models.py.orig 2016-09-19 02:20:40.000000000 +0200
+++ ./forms/models.py 2017-01-26 23:28:42.906541456 +0100
@@ -6,7 +6,7 @@
from mezzanine.conf import settings
from mezzanine.core.fields import RichTextField
-from mezzanine.core.models import Orderable, RichText
+from mezzanine.core.models import Orderable, RichText, wrapped_manager
from mezzanine.forms import fields
from mezzanine.pages.models import Page
@@ -67,7 +67,7 @@
max_length=100)
help_text = models.CharField(_("Help text"), blank=True, max_length=100)
- objects = FieldManager()
+ objects = wrapped_manager(FieldManager)
class Meta:
abstract = True
@wonderbeyond
Copy link

wonderbeyond commented Mar 12, 2020

Hi, can you explain why this commit? Since MT says in documentation:

Every model registered for translation is patched so that all its managers become subclasses of MultilingualManager (of course, if a custom manager was defined on the model, its functions will be retained). MultilingualManager simplifies language-aware queries, especially on third-party apps, by rewriting query field names.

@wonderbeyond
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment