pyoko.db package¶
Submodules¶
pyoko.db.queryset module¶
this module contains a base class for other db access classes
-
class
pyoko.db.queryset.
QuerySet
(**conf)[source]¶ Bases:
object
QuerySet is a lazy data access layer for Riak.
-
__deepcopy__
(memo=None)[source]¶ A deep copy method that doesn’t populate caches and shares Riak client and bucket
-
count
()[source]¶ counts by executing solr query with rows=0 parameter :return: number of objects matches to the query :rtype: int
-
delete
()[source]¶ Deletes all objects that matches to the queryset.
Note
Unlike RDBMS systems, this method makes individual save calls to backend DB store. So this is exists as more of a comfortable utility method and not a performance enhancement.
Returns: List of deleted objects or None if confirm not set. Example
>>> Person.objects.filter(age__gte=16, name__startswith='jo').delete()
-
distinct_values_of
(field)[source]¶ Parameters: field – field name Returns: Distinct values of given field.
-
exclude
(**filters)[source]¶ Applies query filters for excluding matching records from result set.
Parameters: **filters – Query filters as keyword arguments. Returns: Self. Queryset object. Examples
>>> Person.objects.exclude(age=None) >>> Person.objects.filter(name__startswith='jo').exclude(age__lte=16)
-
filter
(**filters)[source]¶ Applies given query filters.
Parameters: **filters – Query filters as keyword arguments. Returns: Self. Queryset object. Examples
>>> Person.objects.filter(name='John') # same as .filter(name__exact='John') >>> Person.objects.filter(age__gte=16, name__startswith='jo') >>> # Assume u1 and u2 as related model instances. >>> Person.objects.filter(work_unit__in=[u1, u2], name__startswith='jo')
-
get
(key=None, **kwargs)[source]¶ Ensures that only one result is returned from DB and raises an exception otherwise. Can work in 3 different way.
- If no argument is given, only does “ensuring about one and only object” job.
- If key given as only argument, retrieves the object from DB.
- if query filters given, implicitly calls filter() method.
Raises: MultipleObjectsReturned
– If there is more than one (1) record is returned.
-
get_or_create
(defaults=None, **kwargs)[source]¶ Looks up an object with the given kwargs, creating a new one if necessary.
Parameters: - defaults (dict) – Used when we create a new object. Must map to fields of the model.
- **kwargs – Used both for filtering and new object creation.
Returns: A tuple of (object, created), where created is a boolean variable specifies whether the object was newly created or not.
Example
In the following example, code and name fields are used to query the DB.
obj, is_new = Permission.objects.get_or_create({'description': desc}, code=code, name=name)
{description: desc} dict is just for new creations. If we can’t find any records by filtering on code and name, then we create a new object by using all of the inputs.
-
or_filter
(**filters)[source]¶ Works like “filter” but joins given filters with OR operator.
Parameters: **filters – Query filters as keyword arguments. Returns: Self. Queryset object. Example
>>> Person.objects.or_filter(age__gte=16, name__startswith='jo')
-
order_by
(*args)[source]¶ Applies query ordering.
Parameters: - **args – Order by fields names.
- to ascending, prepend with hypen (Defaults) –
Returns: Self. Queryset object.
Examples
>>> Person.objects.order_by('-name', 'join_date')
-
save_model
(model, meta_data=None, index_fields=None)[source]¶ saves the model instance to riak :param meta: JSON serializable meta data for logging of save operation.
{‘lorem’: ‘ipsum’, ‘dolar’: 5}Parameters: index_fields (list) – Tuple list for secondary indexing keys in riak (with ‘bin’ or ‘int’). [(‘lorem’,’bin’),(‘dolar’,’int’)] Returns:
-
search_on
(*fields, **query)[source]¶ Search for query on given fields.
- Query modifier can be one of these:
- exact
- contains
- startswith
- endswith
- range
- lte
- gte
Parameters: - *fields (str) – Field list to be searched on
- **query – Search query. While it’s implemented as **kwargs we only support one (first) keyword argument.
Returns: Self. Queryset object.
Examples
>>> Person.objects.search_on('name', 'surname', contains='john') >>> Person.objects.search_on('name', 'surname', startswith='jo')
-
set_model
(model=None, model_class=None)[source]¶ Parameters: - model – Model name
- model_class – Model class
-
update
(**kwargs)[source]¶ Updates the matching objects for specified fields.
Note
Post/pre save hooks and signals will NOT triggered.
Unlike RDBMS systems, this method makes individual save calls to backend DB store. So this is exists as more of a comfortable utility method and not a performance enhancement.
Keyword Arguments: **kwargs – Fields with their corresponding values to be updated. Returns: Int. Number of updated objects. Example
Entry.objects.filter(pub_date__lte=2014).update(comments_on=False)
-
values
(*args)[source]¶ Returns list of dicts (field names as keys) for given fields.
Parameters: *args – List of fields to be returned as dict. Returns: list of dicts for given fields. Example
>>> Person.objects.filter(age__gte=16, name__startswith='jo').values('name', 'lastname')
-
values_list
(*args, **kwargs)[source]¶ Returns list of values for given fields. Since this will implicitly use data() method, it’s more efficient than simply looping through model instances.
Parameters: - flatten (bool) – True. Flatten if there is only one field name given. Returns [‘one’,’two’, ‘three’] instead of [[‘one’], [‘two’], [‘three]]
- *args – List of fields to be retured as list.
Returns: List of deleted objects or None if confirm not set.
Example
>>> Person.objects.filter(age__gte=16).values_list('name', 'lastname')
-
pyoko.db.connection module¶
riak client configuration
pyoko.db.schema_update module¶
-
class
pyoko.db.schema_update.
SchemaUpdater
(registry, bucket_names, threads, force)[source]¶ Bases:
object
traverses trough all models, collects fields marked for index or store in solr then creates a solr schema for these fields.
-
FIELD_TEMPLATE
= '<field type="{type}" name="{name}" indexed="{index}" stored="{store}" multiValued="{multi}" />'¶
-
static
apply_schema
(client, force, job_pack, check_only)[source]¶ riak doesn’t support schema/index updates ( http://git.io/vLOTS )
as a workaround, we create a temporary index, attach it to the bucket, delete the old index/schema, re-create the index with new schema, assign it to bucket, then delete the temporary index.
Parameters: - new_schema (byte) – compiled schema
- bucket_name (str) – name of schema, index and bucket.
Returns: True or False
Return type: bool
-
static
compile_schema
(fields)[source]¶ joins schema fields with base solr schema
Parameters: fields (list[str]) – field list Returns: compiled schema Return type: byte
-