Getting startedΒΆ

You got a model called foobar in an app called someapp, looking like

class Person(models.Model):
    name = models.CharField(max_length = 200)
    age = models.IntegerField(default = 20)
    sex = models.CharField(max_length = 1, choices = (('F', 'Female'), ('M', 'Male')), default = "M")

import dse
from someapp.models import Person

with Person.dse as d:
    # Adding a new item, just defining a name and using the default values from the model:
    d.add_item({'name': 'John'})

    # Overriding the default values? Just specify a valid value
    d.add_item({'name': 'Thomas', 'age': 36, 'sex': 'M'})

    # Update record with id = 1 and set its name to John ( as of version 0.4.0 this will trigger a SQL-statement for this update alone, since not all columns are specified ):
    d.add_item({'id': 1, 'name': 'John'})

To use delayed execution of SQL statements

Person.dse.add_item({'id': 2, 'name': 'Al Capone'}) # will NOT trigger anything
Person.dse.add_item({'id': 3, 'name': 'John Dillinger'}) # will NOT trigger anything
Person.dse.add_item({'name': 'Scarface'}) # will NOT trigger anything
Person.dse.flush() # will execute both update SQL statements and insert a record for "Scarface"

Say you want to update all records with some calculated value, something you couldn`t find a way to do in SQL. Using dse this is easy and fast

with Person.dse as d:
    # Use Djangos ORM to generate dictionaries to use in DSE; objects.all().values().
    for item in Person.objects.all().values():
        item['somevar'] = calculated_value

I`ve recieved some questions about transaction handling. Below is an simple example, but I`m looking into other ways of handling transactions as well

from django.db import transaction
import dse

def some_method():
    with SomeModel.dse as d:
        for item in somelist:
              {'some_column': item.some_value,
               'another_column': item.another_value})

Project Versions

Previous topic


This Page