./manage.py sync_cassandra
Last active
January 23, 2019 12:34
-
-
Save romovpa/be62d133e87adb6a78b07a5d90d4a384 to your computer and use it in GitHub Desktop.
Cassandra in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django_cassandra_engine.models import DjangoCassandraModel | |
class CassandraRouter(object): | |
""" | |
A router to control all database operations on models in the | |
auth application. | |
""" | |
def db_for_read(self, model, **hints): | |
""" | |
Attempts to read auth models go to auth_db. | |
""" | |
if issubclass(model, DjangoCassandraModel): | |
return 'cassandra' | |
return None | |
def db_for_write(self, model, **hints): | |
""" | |
Attempts to write auth models go to auth_db. | |
""" | |
if issubclass(model, DjangoCassandraModel): | |
return 'cassandra' | |
return None | |
def allow_relation(self, obj1, obj2, **hints): | |
""" | |
Allow relations if a model in the auth app is involved. | |
""" | |
if issubclass(obj1.__class__, DjangoCassandraModel) or issubclass(obj2.__class__, DjangoCassandraModel): | |
return issubclass(obj1.__class__, DjangoCassandraModel) == issubclass(obj2.__class__, DjangoCassandraModel) | |
return None | |
def allow_migrate(self, db, app_label, model_name=None, **hints): | |
""" | |
Make sure the auth app only appears in the 'auth_db' | |
database. | |
""" | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSTALLED_APPS = [ | |
..., | |
'django_cassandra_engine', | |
] | |
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.postgresql_psycopg2', | |
'NAME': 'mydb', | |
'USER': 'postgres', | |
'PASSWORD': '123', | |
'HOST': 'localhost', | |
'PORT': '5432', | |
}, | |
'cassandra': { | |
'ENGINE': 'django_cassandra_engine', | |
'NAME': 'db', | |
'TEST_NAME': 'test_db', | |
'HOST': 'localhost', | |
'OPTIONS': { | |
'replication': { | |
'strategy_class': 'SimpleStrategy', | |
'replication_factor': 1 | |
} | |
} | |
}, | |
} | |
DATABASE_ROUTERS = ['db_routers.CassandraRouter'] | |
CASSANDRA_FALLBACK_ORDER_BY_PYTHON = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment