r4fek / django-cassandra-engine

Django Cassandra Engine - the Cassandra backend for Django
BSD 2-Clause "Simplified" License
364 stars 85 forks source link
cassandra django django-cassandra-engine python scylladb

Django Cassandra Engine - the Cassandra backend for Django

All tools you need to start your journey with Apache Cassandra and Django Framework!

Latest version workflow

Discord: https://discord.gg/pxunMGmDNc

Features

Sponsors

Help support ongoing development and maintenance by sponsoring Django Cassandra Engine.

Our Sponsors:

Astra DB
Astra DB

Use Django with DataStax Astra DB - built on Apache Cassandra.
NoiSek
NoiSek

Installation

Recommended installation:

pip install django-cassandra-engine

Basic Usage

  1. Add django_cassandra_engine to INSTALLED_APPS in your settings.py file:

    INSTALLED_APPS = ('django_cassandra_engine',) + INSTALLED_APPS
  2. Change DATABASES setting:

    DATABASES = {
        'default': {
            'ENGINE': 'django_cassandra_engine',
            'NAME': 'db',
            'TEST_NAME': 'test_db',
            'HOST': 'db1.example.com,db2.example.com',
            'OPTIONS': {
                'replication': {
                    'strategy_class': 'SimpleStrategy',
                    'replication_factor': 1
                }
            }
        }
    }
  3. Define some model:

    # myapp/models.py
    
    import uuid
    from cassandra.cqlengine import columns
    from django_cassandra_engine.models import DjangoCassandraModel
    
    class ExampleModel(DjangoCassandraModel):
        example_id    = columns.UUID(primary_key=True, default=uuid.uuid4)
        example_type  = columns.Integer(index=True)
        created_at    = columns.DateTime()
        description   = columns.Text(required=False)
  4. Run ./manage.py sync_cassandra

  5. Done!

Connect to Cassandra with a Cloud Config bundle

To connect to a hosted Cassandra cluster that provides a secure connection bundle (ex. DataStax Astra) change the DATABASES setting of your settings.py:

    DATABASES = {
        'default': {
            'ENGINE': 'django_cassandra_engine',
            'NAME': 'keyspace_name',
            'TEST_NAME': 'table_name',
            'USER': 'token',
            'PASSWORD': token_value,
            'OPTIONS': {
                'connection': {
                    'cloud': {
                        'secure_connect_bundle': '/path/to/secure/bundle.zip'
                    },
                }
            }
        }
    }

Documentation

The documentation can be found online here.

License

Copyright (c) 2014-2024, Rafał Furmański.

All rights reserved. Licensed under BSD 2-Clause License.