Commit e2c6b6c7 authored by Daniel Klaffenbach's avatar Daniel Klaffenbach 🐍

JournalEntryManager: Use `default_manager_name` for newer Django versions

The `use_for_related_fields` manager attribute is deprecated by upstream.
parent b64ce544
Pipeline #13209 passed with stage
in 1 minute and 15 seconds
......@@ -2,6 +2,7 @@
from __future__ import unicode_literals
from getpass import getuser
import logging
from django import VERSION as _DJANGO_VERSION
from django.apps import apps
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
......@@ -19,9 +20,14 @@ from . import _thread_local_storage
logger = logging.getLogger(__name__)
# The `use_for_related_fields` attribute is deprecated in Django 1.10+ and removed in Django 2.0
# This constant allows us to dynamically route code paths depending on the used Django version.
_DJANGO_SUPPORTS_DEFAULT_MANAGER = _DJANGO_VERSION[0:2] >= (1, 10)
class JournalEntryManager(models.Manager):
use_for_related_fields = True
if not _DJANGO_SUPPORTS_DEFAULT_MANAGER:
use_for_related_fields = True
def get_content_type_filter(self, with_children=False, with_parents=False):
if not hasattr(self, 'instance'):
......@@ -100,6 +106,9 @@ class JournalEntry(models.Model):
objects = JournalEntryManager()
class Meta:
if _DJANGO_SUPPORTS_DEFAULT_MANAGER:
default_manager_name = 'objects'
# Uses the deprecated `Manager.use_for_related_fields` as a fallback
ordering = ('id',)
def __str__(self):
......
......@@ -67,6 +67,11 @@ class JournalEntryTestCase(TransactionTestCase):
}
self.assertEqual(updated_entry.get_diff(), expected_diff)
# Tests if the related manager methods are present
self.assertGreater(obj.journal.with_children().count(), 1)
self.assertGreater(obj.journal.with_parents().count(), 1)
def test_journal_entry_operation(self):
""" Tests if the `operation` attribute is set correctly."""
obj = self._create_test_model()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment