fix doc formatting (enumfield, etc)

This commit is contained in:
Bastien Gerard 2021-06-13 23:01:20 +02:00
parent 7bf7153e4f
commit e7f2d77a75
6 changed files with 54 additions and 33 deletions

View File

@ -75,6 +75,7 @@ Fields
.. autoclass:: mongoengine.fields.StringField .. autoclass:: mongoengine.fields.StringField
.. autoclass:: mongoengine.fields.URLField .. autoclass:: mongoengine.fields.URLField
.. autoclass:: mongoengine.fields.EmailField .. autoclass:: mongoengine.fields.EmailField
.. autoclass:: mongoengine.fields.EnumField
.. autoclass:: mongoengine.fields.IntField .. autoclass:: mongoengine.fields.IntField
.. autoclass:: mongoengine.fields.LongField .. autoclass:: mongoengine.fields.LongField
.. autoclass:: mongoengine.fields.FloatField .. autoclass:: mongoengine.fields.FloatField

View File

@ -336,7 +336,6 @@ supplying the :attr:`reverse_delete_rule` attributes on the
:class:`ReferenceField` definition, like this:: :class:`ReferenceField` definition, like this::
class ProfilePage(Document): class ProfilePage(Document):
...
employee = ReferenceField('Employee', reverse_delete_rule=mongoengine.CASCADE) employee = ReferenceField('Employee', reverse_delete_rule=mongoengine.CASCADE)
The declaration in this example means that when an :class:`Employee` object is The declaration in this example means that when an :class:`Employee` object is

View File

@ -181,8 +181,7 @@ def register_connection(
): ):
"""Register the connection settings. """Register the connection settings.
: param alias: the name that will be used to refer to this connection :param alias: the name that will be used to refer to this connection throughout MongoEngine
throughout MongoEngine
:param db: the name of the database to use, for compatibility with connect :param db: the name of the database to use, for compatibility with connect
:param name: the name of the specific database to use :param name: the name of the specific database to use
:param host: the host name of the: program: `mongod` instance to connect to :param host: the host name of the: program: `mongod` instance to connect to

View File

@ -177,14 +177,28 @@ class query_counter:
This was designed for debugging purpose. In fact it is a global counter so queries issued by other threads/processes This was designed for debugging purpose. In fact it is a global counter so queries issued by other threads/processes
can interfere with it can interfere with it
Usage:
.. code-block:: python
class User(Document):
name = StringField()
with query_counter() as q:
user = User(name='Bob')
assert q == 0 # no query fired yet
user.save()
assert q == 1 # 1 query was fired, an 'insert'
user_bis = User.objects().first()
assert q == 2 # a 2nd query was fired, a 'find_one'
Be aware that: Be aware that:
- Iterating over large amount of documents (>101) makes pymongo issue `getmore` queries to fetch the next batch of
documents (https://docs.mongodb.com/manual/tutorial/iterate-a-cursor/#cursor-batches) - Iterating over large amount of documents (>101) makes pymongo issue `getmore` queries to fetch the next batch of documents (https://docs.mongodb.com/manual/tutorial/iterate-a-cursor/#cursor-batches)
- Some queries are ignored by default by the counter (killcursors, db.system.indexes) - Some queries are ignored by default by the counter (killcursors, db.system.indexes)
""" """
def __init__(self, alias=DEFAULT_CONNECTION_NAME): def __init__(self, alias=DEFAULT_CONNECTION_NAME):
"""Construct the query_counter"""
self.db = get_db(alias=alias) self.db = get_db(alias=alias)
self.initial_profiling_level = None self.initial_profiling_level = None
self._ctx_query_counter = 0 # number of queries issued by the context self._ctx_query_counter = 0 # number of queries issued by the context

View File

@ -1611,11 +1611,14 @@ class EnumField(BaseField):
"""Enumeration Field. Values are stored underneath as is, """Enumeration Field. Values are stored underneath as is,
so it will only work with simple types (str, int, etc) that so it will only work with simple types (str, int, etc) that
are bson encodable are bson encodable
Example usage: Example usage:
.. code-block:: python .. code-block:: python
class Status(Enum): class Status(Enum):
NEW = 'new' NEW = 'new'
ONGOING = 'ongoing'
DONE = 'done' DONE = 'done'
class ModelWithEnum(Document): class ModelWithEnum(Document):
@ -1625,13 +1628,18 @@ class EnumField(BaseField):
ModelWithEnum(status=Status.DONE) ModelWithEnum(status=Status.DONE)
Enum fields can be searched using enum or its value: Enum fields can be searched using enum or its value:
.. code-block:: python .. code-block:: python
ModelWithEnum.objects(status='new').count() ModelWithEnum.objects(status='new').count()
ModelWithEnum.objects(status=Status.NEW).count() ModelWithEnum.objects(status=Status.NEW).count()
Note that choices cannot be set explicitly, they are derived The values can be restricted to a subset of the enum by using the ``choices`` parameter:
from the provided enum class.
.. code-block:: python
class ModelWithEnum(Document):
status = EnumField(Status, choices=[Status.NEW, Status.DONE])
""" """
def __init__(self, enum, **kwargs): def __init__(self, enum, **kwargs):

View File

@ -2077,7 +2077,7 @@ class TestField(MongoDBTestCase):
a ComplexBaseField. a ComplexBaseField.
""" """
class EnumField(BaseField): class SomeField(BaseField):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
@ -2088,7 +2088,7 @@ class TestField(MongoDBTestCase):
return tuple(value) return tuple(value)
class TestDoc(Document): class TestDoc(Document):
items = ListField(EnumField()) items = ListField(SomeField())
TestDoc.drop_collection() TestDoc.drop_collection()