def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
        op = is_next and 'gt' or 'lt'
        order = not is_next and '-' or ''
        param = smart_str(getattr(self, field.attname))
        q = Q(**{'%s__%s' % (field.name, op): param})
        q = q|Q(**{field.name: param, 'pk__%s' % op: self.pk})
        qs = self.__class__._default_manager.filter(**kwargs).filter(q).order_by('%s%s' % (order, field.name), '%spk' % order)
        try:
            return qs[0]
        except __HOLE__:
            raise self.DoesNotExist, "%s matching query does not exist." % self.__class__._meta.object_name
dataset/ETHPy150Open CollabQ/CollabQ/vendor/django/db/models/base.py/Model._get_next_or_previous_by_FIELD