3. API References¶
3.1. Basic¶
- class mongu.Client(*args, **kwargs)¶
For Connecting to MongoDB and registering model classes.
- enable_counter(base=None, database='counter', collection='counters')¶
Register the builtin counter model, return the registered Counter class and the corresponding CounterMixin class.
The CounterMixin automatically increases and decreases the counter after model creation(save without _id) and deletion.
It contains a classmethod count() which returns the current count of the model collection.
- register_model(model_cls)¶
Decorator for registering model.
3.2. Extra¶
- class mongu.ObjectDict¶
Makes a dictionary behave like an object, with attribute-style access.
3.3. Base Model¶
- class mongu.Model(*args, **kwargs)¶
Dict-like class with optional default key-values that binds to a collection.
- classmethod by_id(oid)¶
Find a model object by its ObjectId, oid can be string or ObjectId
- classmethod delete_by_id(oid)¶
Delete a document from collection by its ObjectId, oid can be string or ObjectId
- classmethod from_dict(d)¶
Build model object from a dict. Will be removed in v1.0
- classmethod from_cursor(cursor)¶
Build model object from a pymongo cursor.
- classmethod find(*args, **kwargs)¶
Same as collection.find, return model object instead of simple dict.
- classmethod find_one(*args, **kwargs)¶
Same as collection.find_one, return model object instead of simple dict.
- id¶
String representation of attribute _id.
- reload(d=None)¶
Reload model from given dict or database.
- on_save(old_dict)¶
Hook after save.
- save()¶
Save model object to database.
- on_delete(deleted_obj)¶
Hook after delete successful.
- delete()¶
Remove from database.
3.4. Builtin Counter¶
For more information: What and Why
- class mongu.Counter(*args, **kwargs)¶
Builtin counter model.
- classmethod set_to(name, num)¶
Set counter of name to num.
- classmethod change_by(name, num)¶
Change counter of name by num (can be negative).
- classmethod increase(name)¶
Increase counter of name by one.
- classmethod decrease(name)¶
Decrease counter of name by one.
- classmethod count(name)¶
Return the count of name