Try to fix custom manager in migrations (ref #330, #339, #350).

This commit is contained in:
Jacek Tomaszewski 2015-12-13 15:47:01 +01:00
parent a4b9f42d52
commit e50b0ce7a4
4 changed files with 58 additions and 3 deletions

View file

@ -27,6 +27,16 @@ except ImportError:
from django.db.models.loading import AppCache
NEW_APP_CACHE = False
try:
from unittest import skipUnless
except ImportError:
# Dummy replacement for Python 2.6
def skipUnless(condition, reason):
if not condition:
def decorator(test_item):
return lambda s: 42
return decorator
return lambda x: x # identity
from modeltranslation import admin, settings as mt_settings, translator
from modeltranslation.forms import TranslationModelForm
@ -35,6 +45,8 @@ from modeltranslation.tests.test_settings import TEST_SETTINGS
from modeltranslation.utils import (build_css_class, build_localized_fieldname,
auto_populate, fallbacks)
MIGRATIONS = django.VERSION >= (1, 8)
models = translation = None
# None of the following tests really depend on the content of the request,
@ -42,7 +54,7 @@ models = translation = None
request = None
# How many models are registered for tests.
TEST_MODELS = 29
TEST_MODELS = 29 + (1 if MIGRATIONS else 0)
class reload_override_settings(override_settings):
@ -131,9 +143,14 @@ class ModeltranslationTransactionTestBase(TransactionTestCase):
from modeltranslation.models import handle_translation_registrations
handle_translation_registrations()
# 5. Syncdb (``migrate=False`` in case of south)
# 5. makemigrations (``migrate=False`` in case of south)
from django.db import connections, DEFAULT_DB_ALIAS
cmd = 'syncdb' if django.VERSION < (1, 8) else 'migrate'
if MIGRATIONS:
call_command('makemigrations', verbosity=2, interactive=False,
database=connections[DEFAULT_DB_ALIAS].alias)
# 6. Syncdb (``migrate=False`` in case of south)
cmd = 'migrate' if MIGRATIONS else 'syncdb'
call_command(cmd, verbosity=0, migrate=False, interactive=False, run_syncdb=True,
database=connections[DEFAULT_DB_ALIAS].alias, load_initial_data=False)
@ -2624,6 +2641,20 @@ class TestManager(ModeltranslationTestBase):
qs = models.CustomManagerTestModel.objects.custom_qs()
self.assertIsInstance(qs, MultilingualQuerySet)
@skipUnless(MIGRATIONS, 'migrations not available')
def test_3rd_party_custom_manager(self):
from django.contrib.auth.models import Group, GroupManager
from modeltranslation.manager import MultilingualManager
testmodel_fields = get_field_names(Group)
self.assertIn('name', testmodel_fields)
self.assertIn('name_de', testmodel_fields)
self.assertIn('name_en', testmodel_fields)
self.assertIn('name_en', testmodel_fields)
self.assertIsInstance(Group.objects, MultilingualManager)
self.assertIsInstance(Group.objects, GroupManager)
self.assertIn('get_by_natural_key', dir(Group.objects))
def test_multilingual_queryset_pickling(self):
import pickle
from modeltranslation.manager import MultilingualQuerySet

View file

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from django import VERSION
from django.utils.translation import ugettext_lazy
from modeltranslation.translator import translator, register, TranslationOptions
@ -203,3 +204,13 @@ translator.register(RequiredModel, RequiredTranslationOptions)
@register(DecoratedModel)
class DecoratedTranslationOptions(TranslationOptions):
fields = ('title',)
# ######### 3-rd party with custom manager
if VERSION >= (1, 8):
from django.contrib.auth.models import Group
@register(Group)
class GroupTranslationOptions(TranslationOptions):
fields = ('name',)

View file

@ -189,6 +189,18 @@ def add_manager(model):
MultilingualQuerysetManager):
use_for_related_fields = getattr(
manager.__class__, "use_for_related_fields", not has_custom_queryset(manager))
_old_module = manager.__module__
_old_class = manager.__class__.__name__
def deconstruct(self):
return (
False, # as_manager
'%s.%s' % (self._old_module, self._old_class), # manager_class
None, # qs_class
self._constructor_args[0], # args
self._constructor_args[1], # kwargs
)
manager.__class__ = NewMultilingualManager
for _, attname, cls in model._meta.concrete_managers + model._meta.abstract_managers:

View file

@ -37,6 +37,7 @@ def runtests():
DATABASES=DATABASES,
INSTALLED_APPS=(
'django.contrib.contenttypes',
'django.contrib.auth',
'modeltranslation',
),
ROOT_URLCONF=None, # tests override urlconf, but it still needs to be defined