Code

Ticket #19881: get_next_using_manager.patch

File get_next_using_manager.patch, 1.5 KB (added by nkryptic, 14 months ago)

possible alternative that allows use of alternate manager

  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index 5f05865..80422ee 100644
    a b class Model(six.with_metaclass(ModelBase)): 
    666666 
    667667    delete.alters_data = True 
    668668 
     669    def _get_manager_or_default(self, attr=None): 
     670        if attr and hasattr(self.__class__, attr): 
     671            manager = getattr(self.__class__, attr) 
     672        else: 
     673            manager = self.__class__._default_manager 
     674        return manager 
     675 
    669676    def _get_FIELD_display(self, field): 
    670677        value = getattr(self, field.attname) 
    671678        return force_text(dict(field.flatchoices).get(value, value), strings_only=True) 
    class Model(six.with_metaclass(ModelBase)): 
    675682            raise ValueError("get_next/get_previous cannot be used on unsaved objects.") 
    676683        op = is_next and 'gt' or 'lt' 
    677684        order = not is_next and '-' or '' 
     685        using_manager = kwargs.pop('using_manager', None) 
     686        manager = self._get_manager_or_default(using_manager) 
    678687        param = force_text(getattr(self, field.attname)) 
    679688        q = Q(**{'%s__%s' % (field.name, op): param}) 
    680689        q = q | Q(**{field.name: param, 'pk__%s' % op: self.pk}) 
    681         qs = self.__class__._default_manager.using(self._state.db).filter(**kwargs).filter(q).order_by('%s%s' % (order, field.name), '%spk' % order) 
     690        qs = manager.using(self._state.db).filter(**kwargs).filter(q).order_by('%s%s' % (order, field.name), '%spk' % order) 
    682691        try: 
    683692            return qs[0] 
    684693        except IndexError: