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
-
all
(**filters)[source]¶ Applies given query filters and returns all results regardless of result count.
Parameters: **filters – Query filters as keyword arguments. Returns: Self. Queryset object.
-
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()
-
delete_if_exists
(**kwargs)[source]¶ Deletes an object if it exists in database according to given query parameters and returns True otherwise does nothing and returns False.
Parameters: **kwargs – query parameters Returns(bool): True or False
-
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
(all_records=False, **filters)[source]¶ Applies given query filters. If wanted result is more than specified size, exception is raised about using all() method instead of filter.
Parameters: - all_records (bool) –
- **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.
-
get_or_none
(**kwargs)[source]¶ Gets an object if it exists in database according to given query parameters otherwise returns None.
Parameters: **kwargs – query parameters Returns: object or None
-
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
Parameters: - meta (dict) – JSON serializable meta data for logging of save operation. {‘lorem’: ‘ipsum’, ‘dolar’: 5}
- 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
(models, 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
compile_schema
(fields)[source]¶ joins schema fields with base solr schema
Parameters: fields (list[str]) – field list Returns: compiled schema Return type: byte
-
create_index
(model, waiting_models)[source]¶ Creates search indexes.
Parameters: - model – model to execute
- waiting_models – if riak can’t return response immediately, model is taken to queue.
- first execution session, method is executed with waiting models and controlled. (After) –
- be ensured that all given models are executed properly. (And) –
Returns:
-
create_schema
(model, waiting_models)[source]¶ Creates search schemas.
Parameters: - model – model to execute
- waiting_models – if riak can’t return response immediately, model is taken to queue.
- first execution session, method is executed with waiting models and controlled. (After) –
- be ensured that all given models are executed properly. (And) –
Returns:
-
creating_schema_and_index
(models, func)[source]¶ Executes given functions with given models.
Parameters: - models – models to execute
- func – function name to execute
Returns:
-
find_models_and_delete_search_index
(model, force, exec_models, check_only)[source]¶ Finds models to execute and these models’ exist search indexes are deleted. For other operations, necessary models are gathered to list(exec_models)
Parameters: - model – model to execute
- force (bool) – True or False if True, all given models are executed.
- exec_models (list) – if not force, models to execute are gathered to list.
- there is not necessity to migrate operation model doesn't put to exec list. (If) –
- check_only – do not migrate, only report migration is needed or not if True
Returns:
-