Added Custom Objects Managers
Managers can now be directly declared in a Document eg::
```python
class CustomQuerySetManager(QuerySetManager):
@staticmethod
def get_queryset(doc_cls, queryset):
return queryset(is_published=True)
class Post(Document):
is_published = BooleanField(default=False)
published = CustomQuerySetManager()
```
Refactored the name of the `_manager_func` to `get_queryset` to mark it as
part the public API. If declaring a Manager with a get_queryset method, it
should be a staticmethod, that accepts the document_class and the queryset.
Note - you can still use decorators in fact in the example below,
we effectively do the same thing as the first example and is much less verbose.
```python
class Post(Document):
is_published = BooleanField(default=False)
@queryset_manager
def published(doc_cls, queryset):
return queryset(is_published=True)
```
Thanks to @theojulienne for the initial impetus and code sample #108
This commit is contained in:
@@ -299,8 +299,10 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||
new_class = super_new(cls, name, bases, attrs)
|
||||
|
||||
# Provide a default queryset unless one has been manually provided
|
||||
if not hasattr(new_class, 'objects'):
|
||||
new_class.objects = QuerySetManager()
|
||||
manager = attrs.get('objects', QuerySetManager())
|
||||
if hasattr(manager, 'queryset_class'):
|
||||
meta['queryset_class'] = manager.queryset_class
|
||||
new_class.objects = manager
|
||||
|
||||
user_indexes = [QuerySet._build_index_spec(new_class, spec)
|
||||
for spec in meta['indexes']] + base_indexes
|
||||
|
||||
Reference in New Issue
Block a user