Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Python Driver. Switch to the latest stable version.

Scylla Python Driver API Documentation cassandra.cqlengine.models - Table models for object mapping

cassandra.cqlengine.models - Table models for object mapping¶

Model¶

class cassandra.cqlengine.models.Model(\*\*kwargs)¶

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.

__table_name_case_sensitive__ = False¶

Optional. By default, __table_name__ is case insensitive. Set this to True if you want to preserve the case sensitivity.

__keyspace__ = None¶

Sets the name of the keyspace used by this model.

__connection__ = None¶

Sets the name of the default connection used by this model.

__default_ttl__ = None¶

Will be deprecated in release 4.0. You can set the default ttl by configuring the table __options__. See Default TTL and Per Query TTL for more details.

__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, including compaction. Unspecified, these default to sensible values in the server. To override defaults, set options using the model __options__ attribute, which allows options specified a dict.

When a table is synced, it will be altered to match the 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 options settings of cqlengine if you want to manage your compaction settings manually.

See the list of supported table properties for more information.

__options__¶

For example:

class User(Model):
    __options__ = {'compaction': {'class': 'LeveledCompactionStrategy',
                                  'sstable_size_in_mb': '64',
                                  'tombstone_threshold': '.2'},
                   'comment': 'User data stored here'}

    user_id = columns.UUID(primary_key=True)
    name = columns.Text()

or :

class TimeData(Model):
    __options__ = {'compaction': {'class': 'SizeTieredCompactionStrategy',
                                  'bucket_low': '.3',
                                  'bucket_high': '2',
                                  'min_threshold': '2',
                                  'max_threshold': '64',
                                  'tombstone_compaction_interval': '86400'},
                   'gc_grace_seconds': '0'}
__compute_routing_key__ = True¶

Optional Setting False disables computing the routing key for TokenAwareRouting

The base methods allow creating, storing, and querying modeled objects.

classmethod create(**kwargs)¶

Create an instance of this model in the database.

Takes the model column values as keyword arguments. Setting a value to None is equivalent to running a CQL DELETE on that column.

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 isn’t applied, a LWTException is raised.

try:
    TestIfNotExistsModel.if_not_exists().create(id=id, count=9, text='111111111111')
except LWTException as e:
    # handle failure case
    print e.existing  # dict containing LWT result fields

This method is supported on Cassandra 2.0 or later.

if_exists()¶

Check the existence of an object before an update or delete. The existence of an object is determined by its primary key(s). And please note using this flag would incur performance cost.

If the update or delete isn’t applied, a LWTException is raised.

try:
    TestIfExistsModel.objects(id=id).if_exists().update(count=9, text='111111111111')
except LWTException as e:
    # handle failure case
    pass

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. Setting a value to None is equivalent to running a CQL DELETE on that column.

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 case
    print e.existing # existing object
classmethod 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.

classmethod filter(*args, **kwargs)¶

Returns a queryset based on filter parameters.

This is a pass-through to the model objects().:method:~cqlengine.queries.filter.

classmethod 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

timeout(timeout)¶

Sets a timeout for use in save(), update(), and delete() operations

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.

using(connection=None)¶

Change the context on the fly of the model instance (keyspace, connection)

classmethod 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.

PREVIOUS
cassandra.io.twistedreactor - Twisted Event Loop
NEXT
cassandra.cqlengine.columns - Column types for object mapping models
  • 3.24.8
    • 3.25.4
    • 3.24.8
    • 3.22.3
    • 3.21.0
  • API Documentation
    • cassandra - Exceptions and Enums
    • cassandra.cluster - Clusters and Sessions
    • cassandra.policies - Load balancing and Failure Handling Policies
    • cassandra.auth - Authentication
    • cassandra.graph - Graph Statements, Options, and Row Factories
    • cassandra.metadata - Schema and Ring Topology
    • cassandra.metrics - Performance Metrics
    • cassandra.query - Prepared Statements, Batch Statements, Tracing, and Row Factories
    • cassandra.pool - Hosts and Connection Pools
    • cassandra.protocol - Protocol Features
    • cassandra.encoder - Encoders for non-prepared Statements
    • cassandra.decoder - Data Return Formats
    • cassandra.concurrent - Utilities for Concurrent Statement Execution
    • cassandra.connection - Low Level Connection Info
    • cassandra.util - Utilities
    • cassandra.timestamps - Timestamp Generation
    • cassandra.io.asyncioreactor - asyncio Event Loop
    • cassandra.io.asyncorereactor - asyncore Event Loop
    • cassandra.io.eventletreactor - eventlet-compatible Connection
    • cassandra.io.libevreactor - libev Event Loop
    • cassandra.io.geventreactor - gevent-compatible Event Loop
    • cassandra.io.twistedreactor - Twisted Event Loop
    • cassandra.cqlengine.models - Table models for object mapping
    • cassandra.cqlengine.columns - Column types for object mapping models
    • cassandra.cqlengine.query - Query and filter model objects
    • cassandra.cqlengine.connection - Connection management for cqlengine
    • cassandra.cqlengine.management - Schema management for cqlengine
    • cassandra.cqlengine.usertype - Model classes for User Defined Types
    • cassandra.datastax.graph - Graph Statements, Options, and Row Factories
    • cassandra.datastax.graph.fluent
    • cassandra.datastax.graph.fluent.query
    • cassandra.datastax.graph.fluent.predicates
  • Installation
  • Getting Started
  • Scylla Specific Features
  • Upgrading
  • Execution Profiles
  • Performance Notes
  • Paging Large Queries
  • Lightweight Transactions (Compare-and-set)
  • Security
  • User Defined Types
  • Object Mapper
    • Upgrade Guide
    • Models
    • Making Queries
    • Batch Queries
    • Connections
    • Third party integrations
    • Frequently Asked Questions
  • Working with Dates and Times
  • Scylla Cloud
  • Frequently Asked Questions
  • Create an issue
  • Edit this page

On this page

  • cassandra.cqlengine.models - Table models for object mapping
    • Model
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© ScyllaDB 2021 and © DataStax 2013-2017
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2