syncano.models.manager¶
- class Manager[source]¶
Bases: syncano.connection.ConnectionMixin
Base class responsible for all ORM (please) actions.
- BATCH_URI = '/v1.1/instances/{name}/batch/'¶
- batch(*args)[source]¶
A convenience method for making a batch request. Only create, update and delete manager method are supported. Batch request are limited to 50. So the args length should be equal or less than 50.
Usage:
klass = instance.classes.get(name='some_class') Object.please.batch( klass.objects.as_batch().delete(id=652), klass.objects.as_batch().delete(id=653), ... )
and:
Object.please.batch( klass.objects.as_batch().update(id=652, arg='some_b'), klass.objects.as_batch().update(id=653, arg='some_b'), ... )
and:
Object.please.batch( klass.objects.as_batch().create(arg='some_c'), klass.objects.as_batch().create(arg='some_c'), ... )
and:
Object.please.batch( klass.objects.as_batch().delete(id=653), klass.objects.as_batch().update(id=652, arg='some_a'), klass.objects.as_batch().create(arg='some_c'), ... )
are posible.
But:
Object.please.batch( klass.objects.as_batch().get_or_create(id=653, arg='some_a') )
will not work as expected.
Some snippet for working with instance users:
instance = Instance.please.get(name='Nabuchodonozor') model_users = instance.users.batch( instance.users.as_batch().delete(id=7), instance.users.as_batch().update(id=9, username='username_a'), instance.users.as_batch().create(username='username_b', password='5432'), ... )
And sample response will be:
[{u'code': 204}, <User: 9>, <User: 11>, ...]
Parameters: args – a arg is on of the: klass.objects.as_batch().create(...), klass.objects.as_batch().update(...), klass.objects.as_batch().delete(...) Returns: a list with objects corresponding to batch arguments; update and create will return a populated Object, when delete return a raw response from server (usually a dict: {‘code’: 204}, sometimes information about not found resource to delete);
- create(**kwargs)[source]¶
A convenience method for creating an object and saving it all in one step. Thus:
instance = Instance.please.create(name='test-one', description='description')
and:
instance = Instance(name='test-one', description='description') instance.save()
are equivalent.
- bulk_create(*objects)[source]¶
Creates many new instances based on provided list of objects.
Usage:
instance = Instance.please.get(name='instance_a') instances = instance.users.bulk_create( User(username='user_a', password='1234'), User(username='user_b', password='4321') )
Warning:
This method is restricted to handle 50 objects at once.
- get(*args, **kwargs)[source]¶
Returns the object matching the given lookup parameters.
Usage:
instance = Instance.please.get('test-one') instance = Instance.please.get(name='test-one')
- in_bulk(*args, **kwargs)[source]¶
A method which allows to bulk get objects;
Use:
response = Classes.please.in_bulk(['test_class', ...])
response is:
> {‘test_class’: <Class: test_class>}For objects:
res = Object.please.in_bulk([1, 2], class_name=’test_class’)or
res = klass.objects.in_bulk([1, 2])response is:
{1: <SyncanoTestClassObject: 1>, 2: {u’content’: {u’detail’: u’Not found.’}, u’code’: 404}}Parameters: object_ids_list – This list expects the primary keys - id in api, a names, ids can be used here; Returns: a dict in which keys are the object_ids_list elements, and values are a populated objects;
- detail(*args, **kwargs)[source]¶
Wrapper around get method.
Usage:
instance = Instance.please.detail('test-one') instance = Instance.please.detail(name='test-one')
- get_or_create(**kwargs)[source]¶
A convenience method for looking up an object with the given lookup parameters, creating one if necessary.
Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.
This is meant as a shortcut to boilerplatish code. For example:
try: instance = Instance.please.get(name='test-one') except Instance.DoesNotExist: instance = Instance(name='test-one', description='test') instance.save()
The above example can be rewritten using get_or_create() like so:
instance, created = Instance.please.get_or_create(name='test-one', defaults={'description': 'test'})
- delete(*args, **kwargs)[source]¶
Removes single instance based on provided arguments. Returns None if deletion went fine.
Usage:
Instance.please.delete('test-one') Instance.please.delete(name='test-one')
- new_update(*args, **kwargs)[source]¶
Updates multiple instances based on provided arguments. There to ways to do so:
- Django-style update.
- By specifying arguments.
Usage:
objects = Object.please.list(instance_name=INSTANCE_NAME, class_name='someclass').filter(id=1).update(arg='103') objects = Object.please.list(instance_name=INSTANCE_NAME, class_name='someclass').filter(id=1).update(arg='103')
The return value is a list of objects;
- old_update(*args, **kwargs)[source]¶
Updates single instance based on provided arguments. There to ways to do so:
- Django-style update.
- By specifying data argument.
The data is a dictionary of (field, value) pairs used to update the object.
Usage:
instance = Instance.please.update('test-one', description='new one') instance = Instance.please.update(name='test-one', description='new one') instance = Instance.please.update('test-one', data={'description': 'new one'}) instance = Instance.please.update(name='test-one', data={'description': 'new one'})
- update_or_create(defaults=None, **kwargs)[source]¶
A convenience method for updating an object with the given parameters, creating a new one if necessary. The defaults is a dictionary of (field, value) pairs used to update the object.
Returns a tuple of (object, created), where object is the created or updated object and created is a boolean specifying whether a new object was created.
The update_or_create method tries to fetch an object from Syncano API based on the given kwargs. If a match is found, it updates the fields passed in the defaults dictionary.
This is meant as a shortcut to boilerplatish code. For example:
try: instance = Instance.please.update(name='test-one', data=updated_values) except Instance.DoesNotExist: updated_values.update({'name': 'test-one'}) instance = Instance(**updated_values) instance.save()
This pattern gets quite unwieldy as the number of fields in a model goes up. The above example can be rewritten using update_or_create() like so:
instance, created = Instance.please.update_or_create(name='test-one', defaults=updated_values)
- all(*args, **kwargs)[source]¶
Returns a copy of the current Manager with limit removed.
Usage:
instances = Instance.please.all()
- list(*args, **kwargs)[source]¶
Returns a copy of the current Manager containing objects that match the given lookup parameters.
- Usage::
- instance = Instance.please.list() classes = Class.please.list(instance_name=’test-one’)
- first(*args, **kwargs)[source]¶
Returns the first object matched by the lookup parameters or None, if there is no matching object.
Usage:
instance = Instance.please.first() classes = Class.please.first(instance_name='test-one')
- page_size(*args, **kwargs)[source]¶
Sets page size.
Usage:
instances = Instance.please.page_size(20).all()
- limit(*args, **kwargs)[source]¶
Sets limit of returned objects.
Usage:
instances = Instance.please.list().limit(10) classes = Class.please.list(instance_name='test-one').limit(10)
- ordering(*args, **kwargs)[source]¶
Sets order of returned objects.
Usage:
instances = Instance.please.ordering()
- raw(*args, **kwargs)[source]¶
Disables serialization. request method will return raw Python types.
Usage:
>>> instances = Instance.please.list().raw() >>> instances [{'description': 'new one', 'name': 'test-one'...}...]
- template(*args, **kwargs)[source]¶
Disables serialization. request method will return raw text.
Usage:
>>> instances = Instance.please.list().template('test') >>> instances u'text'
- class ScriptManager[source]¶
Bases: syncano.models.manager.Manager
Custom Manager class for Script model.
- class ScriptEndpointManager[source]¶
Bases: syncano.models.manager.Manager
Custom Manager class for ScriptEndpoint model.
- class ObjectManager[source]¶
Bases: syncano.models.manager_mixins.IncrementMixin, syncano.models.manager_mixins.ArrayOperationsMixin, syncano.models.manager.Manager
Custom Manager class for Object model.
- LOOKUP_SEPARATOR = '__'¶
- ALLOWED_LOOKUPS = ['gt', 'gte', 'lt', 'lte', 'eq', 'neq', 'exists', 'in', 'nin', 'near', 'is', 'contains', 'startswith', 'endswith', 'contains', 'istartswith', 'iendswith', 'icontains', 'ieq', 'near']¶
- count(*args, **kwargs)[source]¶
Return the queryset count;
Usage:
Object.please.list(instance_name='raptor', class_name='some_class').filter(id__gt=600).count() Object.please.list(instance_name='raptor', class_name='some_class').count() Object.please.all(instance_name='raptor', class_name='some_class').count()
Returns: The count of the returned objects: count = DataObjects.please.list(...).count();
- with_count(*args, **kwargs)[source]¶
Return the queryset with count;
Usage:
Object.please.list(instance_name='raptor', class_name='some_class').filter(id__gt=600).with_count() Object.please.list(instance_name='raptor', class_name='some_class').with_count(page_size=30) Object.please.all(instance_name='raptor', class_name='some_class').with_count()
Parameters: page_size – The size of the pagination; Default to 20; Returns: The tuple with objects and the count: objects, count = DataObjects.please.list(...).with_count();
- filter(*args, **kwargs)[source]¶
Special method just for data object Object model.
Usage:
objects = Object.please.list('instance-name', 'class-name').filter(henryk__gte='hello')
- bulk_create(*objects)[source]¶
Creates many new objects. Usage:
created_objects = Object.please.bulk_create( Object(instance_name='instance_a', class_name='some_class', title='one'), Object(instance_name='instance_a', class_name='some_class', title='two'), Object(instance_name='instance_a', class_name='some_class', title='three') )
Parameters: objects – a list of the instances of data objects to be created; Returns: a created and populated list of objects; When error occurs a plain dict is returned in that place;
- fields(*args, **kwargs)[source]¶
Special method just for data object Object model.
Usage:
objects = Object.please.list('instance-name', 'class-name').fields('name', 'id')
- class SchemaManager(schema=None)[source]¶
Bases: object
Custom Manager class for SchemaFiled.
- set_index(field, order=False, filter=False)[source]¶
Sets index on selected field.
Parameters: - field (string) – Name of schema field
- filter (bool) – Sets filter index on selected field
- order (bool) – Sets order index on selected field