newid(obj,
id_func,
noUuid=False,
noType=False,
sep=':')
| source code
|
Helper function to make document IDs more readable.
By default, CouchableDb document IDs have the following form:
module.Class:UUID
The intent is that each document ID will be reasonably easy to read
and identify at a glance. However, for some document classes, there is a
more appropriate way to label each instance. For example, a
Person class might want to include first and last name as
part of the ID, so that casual examination of the document ID makes it
clear which person that ID corresponds to.
newid has no return data; it sets the _id on the
object if one is not already present.
- Parameters:
obj (object) - The object to potentially set an _id on.
id_func (callable) - A callable that takes the object and returns a string to include
in the _id.
noUuid (bool) - A flag that indicates if a UUID should be appended to the ID.
noType (bool) - A flag that indicates if type information should be prepended to
the ID.
sep (string) - The string join the various ID components with. Defaults to
':'.
Example:
class ClassA(object):
def __init__(self, name):
self.name = name
# ...
couchable.registerDocType(ClassA,
lambda obj, cdb: couchable.newid(obj, lambda x: x.name),
lambda obj, cdb: None)
couchable.newid(ClassA('foo')) == 'example.ClassA:foo:4094b428-5b45-44fe-bd27-dcb173ec98e8'
|