Python Django: Minimal Django + Cassandra local application
Question: I am trying to put together a minimum Django application that uses Cassandra
as the database.

Here is what I tried:

  1. Started a brand new Django project in PyCharm. Checked that `python manage.py runserver` works as expected. 
  2. Installed Cassandra using the instructions [here](http://wiki.apache.org/cassandra/DebianPackaging). I had to change the `rpc_port` and `storage_port` to numbers lower than 9000 to get this to work (probably something to do with my firewall). Cassandra is running, and I am able to execute instructions using `cqlsh`. 
  3. Installed [`cassandra-django-engine`](http://r4fek.github.io/django-cassandra-engine/). 
  4. Made the following changes to the `settings.py` file: 

     * Added `django_cassandra_engine` to the front of the `INSTALLED_APPS` tuple: 
        
                INSTALLED_APPS = (
        'django_cassandra_engine',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        )
        

     * Replaced SQLite as the database to Cassandra, like so: 
        
                DATABASES = {
           'default': {
             'ENGINE': 'django_cassandra_engine',
             'NAME': 'db',
             'TEST_NAME': 'test_db',
             'HOST': 'localhost',
             'PORT': '8160',
             'OPTIONS': {
               'replication': {
               'strategy_class': 'SimpleStrategy',
               'replication_factor': 1
              }
            }
          }
        } 
        

Note that this is the part that I am least sure about. I could not figure out,
based on the documentation what the parameters `NAME` and `TEST_NAME` are
supposed to be. Note also that I have added a `'PORT'` key and that the port
number defaults to `9160` but I have changed it to `8160` for the reasons
mentioned above. Not sure I need to add this key.

# Error information:

Here is a full traceback of the error from running a `python manage.py
runserver` with the above project. Note that there are no apps defined in the
project yet.

    
    
    /usr/local/lib/python3.4/dist-packages/django/db/utils.py:238: RemovedInDjango19Warning: In Django 1.9 the TEST_NAME connection setting will be moved to a NAME entry in the TEST setting
      self.prepare_test_settings(alias)
    
    /usr/local/lib/python3.4/dist-packages/cassandra/util.py:486: UserWarning: The blist library is not available, so a pure python list-based set will be used in place of blist.sortedset for set collection values. You can find the blist library here: https://pypi.python.org/pypi/blist/
      "The blist library is not available, so a pure python list-based set will "
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.4/dist-packages/django_cassandra_engine/models.py", line 7, in <module>
        from uwsgidecorators import postfork
      File "/usr/local/lib/python3.4/dist-packages/uwsgidecorators.py", line 10, in <module>
        import uwsgi
    ImportError: No module named 'uwsgi'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
        utility.execute()
      File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 312, in execute
        django.setup()
      File "/usr/local/lib/python3.4/dist-packages/django/__init__.py", line 18, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 108, in populate
        app_config.import_models(all_models)
      File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line 198, in import_models
        self.models_module = import_module(models_module_name)
      File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
      File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
      File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
      File "<frozen importlib._bootstrap>", line 1129, in _exec
      File "<frozen importlib._bootstrap>", line 1471, in exec_module
      File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
      File "/usr/local/lib/python3.4/dist-packages/django_cassandra_engine/models.py", line 12, in <module>
        cassandra_connection.connect()
      File "/usr/local/lib/python3.4/dist-packages/django_cassandra_engine/base/__init__.py", line 94, in connect
        self.connection = CassandraConnection(**settings)
      File "/usr/local/lib/python3.4/dist-packages/django_cassandra_engine/connection.py", line 52, in __init__
        self.setup()
      File "/usr/local/lib/python3.4/dist-packages/django_cassandra_engine/connection.py", line 59, in setup
        connection.setup(self.hosts, self.keyspace, **self.connection_options)
      File "/usr/local/lib/python3.4/dist-packages/cassandra/cqlengine/connection.py", line 129, in setup
        session = cluster.connect()
      File "/usr/local/lib/python3.4/dist-packages/cassandra/cluster.py", line 755, in connect
        self.control_connection.connect()
      File "/usr/local/lib/python3.4/dist-packages/cassandra/cluster.py", line 1868, in connect
        self._set_new_connection(self._reconnect_internal())
      File "/usr/local/lib/python3.4/dist-packages/cassandra/cluster.py", line 1903, in _reconnect_internal
        raise NoHostAvailable("Unable to connect to any servers", errors)
    cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'localhost': OperationTimedOut('errors=None, last_host=None',)})
    

From what I can understand,  
1\. There is a top level `ImportError`:

    
    
    ImportError: No module named 'uwsgi'
    

2\. And there is a nested error:

    
    
    cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'localhost': OperationTimedOut('errors=None, last_host=None',)})
    

I am not sure which one of the two above is the more relevant error, and how
to resolve the error in either case. Note that I have installed both the
modules `uwsgi` and `uwsgidecorators`.


Answer: So it turns out that the answer was fairly straightforward. Yes, a `'PORT'`
key is required (actually a `'port'` key), but it goes into the `'connection'`
setting, and is set to the `storage_port` and not to the `rpc_port`. Once I
make these changes, the Django server runs cleanly.

