cassandra.cqlengine.models
- Table models for object mapping¶
Model¶
-
class
cassandra.cqlengine.models.
Model
(**kwargs)[source]¶ The initializer creates an instance of the model. Pass in keyword arguments for columns you’ve defined on the model.
class Person(Model): id = columns.UUID(primary_key=True) first_name = columns.Text() last_name = columns.Text() person = Person(first_name='Blake', last_name='Eggleston') person.first_name #returns 'Blake' person.last_name #returns 'Eggleston'
Model attributes define how the model maps to tables in the database. These are class variables that should be set when defining Model deriviatives.
-
__abstract__
= False¶ Optional. Indicates that this model is only intended to be used as a base class for other models. You can’t create tables for abstract models, but checks around schema validity are skipped during class construction.
-
__table_name__
= None¶ Optional. Sets the name of the CQL table for this model. If left blank, the table name will be the name of the model, with it’s module name as it’s prefix. Manually defined table names are not inherited.
-
__keyspace__
= None¶ Sets the name of the keyspace used by this model.
-
__default_ttl__
= None¶ Optional The default ttl used by this model.
This can be overridden by using the
ttl()
method.
-
__polymorphic_key__
= None¶ Deprecated.
-
__discriminator_value__
= None¶ Optional Specifies a value for the discriminator column when using model inheritance.
See Model Inheritance for usage examples.
Each table can have its own set of configuration options. These can be specified on a model with the following attributes:
See the list of supported table properties for more information.
-
__bloom_filter_fp_chance
¶
-
__caching__
¶
-
__comment__
¶
-
__dclocal_read_repair_chance__
¶
-
__default_time_to_live__
¶
-
__gc_grace_seconds__
¶
-
__index_interval__
¶
-
__memtable_flush_period_in_ms__
¶
-
__populate_io_cache_on_flush__
¶
-
__read_repair_chance__
¶
-
__replicate_on_write__
¶
Model presently supports specifying compaction options via class attributes. cqlengine will only use your compaction options if you have a strategy set. When a table is synced, it will be altered to match the compaction options set on your table. This means that if you are changing settings manually they will be changed back on resync.
Do not use the compaction settings of cqlengine if you want to manage your compaction settings manually.
cqlengine supports all compaction options as of Cassandra 1.2.8.
These attibutes will likely be replaced by a single options string in a future version, and are therefore deprecated.
-
__compaction_bucket_high__
Deprecated¶
-
__compaction_bucket_low__
Deprecated¶
-
__compaction_max_compaction_threshold__
Deprecated¶
-
__compaction_min_compaction_threshold__
Deprecated¶
-
__compaction_min_sstable_size__
Deprecated¶
-
__compaction_sstable_size_in_mb__
Deprecated¶
-
__compaction_tombstone_compaction_interval__
Deprecated¶
-
__compaction_tombstone_threshold__
Deprecated¶ For example:
class User(Model): __compaction__ = cqlengine.LeveledCompactionStrategy __compaction_sstable_size_in_mb__ = 64 __compaction_tombstone_threshold__ = .2 user_id = columns.UUID(primary_key=True) name = columns.Text()
or for SizeTieredCompaction:
class TimeData(Model): __compaction__ = SizeTieredCompactionStrategy __compaction_bucket_low__ = .3 __compaction_bucket_high__ = 2 __compaction_min_threshold__ = 2 __compaction_max_threshold__ = 64 __compaction_tombstone_compaction_interval__ = 86400
Tables may use LeveledCompactionStrategy or SizeTieredCompactionStrategy. Both options are available in the top level cqlengine module. To reiterate, you will need to set your __compaction__ option explicitly in order for cqlengine to handle any of your settings.
The base methods allow creating, storing, and querying modeled objects.
-
create
(**kwargs)¶ Create an instance of this model in the database.
Takes the model column values as keyword arguments.
Returns the instance.
-
if_not_exists
()¶ Check the existence of an object before insertion. The existence of an object is determined by its primary key(s). And please note using this flag would incur performance cost.
if the insertion didn’t applied, a LWTException exception would be raised.
try: TestIfNotExistsModel.if_not_exists().create(id=id, count=9, text='111111111111') except LWTException as e: # handle failure case print e.existing # existing object
This method is supported on Cassandra 2.0 or later.
-
save
()¶ Saves an object to the database.
#create a person instance person = Person(first_name='Kimberly', last_name='Eggleston') #saves it to Cassandra person.save()
-
update
(**values)¶ Performs an update on the model instance. You can pass in values to set on the model for updating, or you can call without values to execute an update against any modified fields. If no fields on the model have been modified since loading, no query will be performed. Model validation is performed normally.
It is possible to do a blind update, that is, to update a field without having first selected the object out of the database. See Blind Updates
-
iff
(**values)¶ Checks to ensure that the values specified are correct on the Cassandra cluster. Simply specify the column(s) and the expected value(s). As with if_not_exists, this incurs a performance cost.
If the insertion isn’t applied, a LWTException is raised
t = TestTransactionModel(text='some text', count=5) try: t.iff(count=5).update('other text') except LWTException as e: # handle failure
-
get
(*args, **kwargs)¶ Returns a single object based on the passed filter constraints.
This is a pass-through to the model objects().:method:~cqlengine.queries.get.
-
filter
(*args, **kwargs)¶ Returns a queryset based on filter parameters.
This is a pass-through to the model objects().:method:~cqlengine.queries.filter.
-
all
()¶ Returns a queryset representing all stored objects
This is a pass-through to the model objects().all()
-
delete
()¶ Deletes the object from the database
-
batch
(batch_object)¶ Sets the batch object to run instance updates and inserts queries with.
See Batch Queries for usage examples
-
timestamp
(timedelta_or_datetime)¶ Sets the timestamp for the query
-
ttl
(ttl_in_sec)¶ Sets the ttl values to run instance updates and inserts queries with.
-
column_family_name
(include_keyspace=True)¶ Returns the column family name if it’s been defined otherwise, it creates it from the module and class name
Models also support dict-like access:
-
len
(m)¶ Returns the number of columns defined in the model
-
m[col_name]
Returns the value of column
col_name
-
m[col_name] = value
Set
m[col_name]
to value
-
keys
()¶ Returns a list of column IDs.
-
values
()¶ Returns list of column values.
-
items
()¶ Returns a list of column ID/value tuples.
-