Updates to documentation in prep for 0.5
This commit is contained in:
		| @@ -4,14 +4,14 @@ Defining documents | ||||
| In MongoDB, a **document** is roughly equivalent to a **row** in an RDBMS. When | ||||
| working with relational databases, rows are stored in **tables**, which have a | ||||
| strict **schema** that the rows follow. MongoDB stores documents in | ||||
| **collections** rather than tables - the principle difference is that no schema  | ||||
| is enforced at a database level.  | ||||
| **collections** rather than tables - the principle difference is that no schema | ||||
| is enforced at a database level. | ||||
|  | ||||
| Defining a document's schema | ||||
| ============================ | ||||
| MongoEngine allows you to define schemata for documents as this helps to reduce | ||||
| coding errors, and allows for utility methods to be defined on fields which may | ||||
| be present.  | ||||
| be present. | ||||
|  | ||||
| To define a schema for a document, create a class that inherits from | ||||
| :class:`~mongoengine.Document`. Fields are specified by adding **field | ||||
| @@ -19,7 +19,7 @@ objects** as class attributes to the document class:: | ||||
|  | ||||
|     from mongoengine import * | ||||
|     import datetime | ||||
|      | ||||
|  | ||||
|     class Page(Document): | ||||
|         title = StringField(max_length=200, required=True) | ||||
|         date_modified = DateTimeField(default=datetime.datetime.now) | ||||
| @@ -31,31 +31,34 @@ By default, fields are not required. To make a field mandatory, set the | ||||
| validation constraints available (such as :attr:`max_length` in the example | ||||
| above). Fields may also take default values, which will be used if a value is | ||||
| not provided. Default values may optionally be a callable, which will be called | ||||
| to retrieve the value (such as in the above example). The field types available  | ||||
| to retrieve the value (such as in the above example). The field types available | ||||
| are as follows: | ||||
|  | ||||
| * :class:`~mongoengine.StringField` | ||||
| * :class:`~mongoengine.URLField` | ||||
| * :class:`~mongoengine.EmailField` | ||||
| * :class:`~mongoengine.IntField` | ||||
| * :class:`~mongoengine.FloatField` | ||||
| * :class:`~mongoengine.DecimalField` | ||||
| * :class:`~mongoengine.DateTimeField` | ||||
| * :class:`~mongoengine.ComplexDateTimeField` | ||||
| * :class:`~mongoengine.ListField` | ||||
| * :class:`~mongoengine.SortedListField` | ||||
| * :class:`~mongoengine.DictField` | ||||
| * :class:`~mongoengine.MapField` | ||||
| * :class:`~mongoengine.ObjectIdField` | ||||
| * :class:`~mongoengine.EmbeddedDocumentField` | ||||
| * :class:`~mongoengine.ReferenceField` | ||||
| * :class:`~mongoengine.GenericReferenceField` | ||||
| * :class:`~mongoengine.EmbeddedDocumentField` | ||||
| * :class:`~mongoengine.BooleanField` | ||||
| * :class:`~mongoengine.FileField` | ||||
| * :class:`~mongoengine.EmailField` | ||||
| * :class:`~mongoengine.SortedListField` | ||||
| * :class:`~mongoengine.BinaryField` | ||||
| * :class:`~mongoengine.GeoPointField` | ||||
| * :class:`~mongoengine.SequenceField` | ||||
|  | ||||
| Field arguments | ||||
| --------------- | ||||
| Each field type can be customized by keyword arguments.  The following keyword  | ||||
| Each field type can be customized by keyword arguments.  The following keyword | ||||
| arguments can be set on all fields: | ||||
|  | ||||
| :attr:`db_field` (Default: None) | ||||
| @@ -74,7 +77,7 @@ arguments can be set on all fields: | ||||
|  | ||||
|     The definion of default parameters follow `the general rules on Python | ||||
|     <http://docs.python.org/reference/compound_stmts.html#function-definitions>`__, | ||||
|     which means that some care should be taken when dealing with default mutable objects  | ||||
|     which means that some care should be taken when dealing with default mutable objects | ||||
|     (like in :class:`~mongoengine.ListField` or :class:`~mongoengine.DictField`):: | ||||
|  | ||||
|         class ExampleFirst(Document): | ||||
| @@ -89,7 +92,7 @@ arguments can be set on all fields: | ||||
|             # This can make an .append call to  add values to the default (and all the following objects), | ||||
|             # instead to just an object | ||||
|             values = ListField(IntField(), default=[1,2,3]) | ||||
|      | ||||
|  | ||||
|  | ||||
| :attr:`unique` (Default: False) | ||||
|     When True, no documents in the collection will have the same value for this | ||||
| @@ -104,7 +107,13 @@ arguments can be set on all fields: | ||||
|  | ||||
| :attr:`choices` (Default: None) | ||||
|     An iterable of choices to which the value of this field should be limited. | ||||
|      | ||||
|  | ||||
| :attr:`help_text` (Default: None) | ||||
|     Optional help text to output with the field - used by form libraries | ||||
|  | ||||
| :attr:`verbose` (Default: None) | ||||
|     Optional human-readable name for the field - used by form libraries | ||||
|  | ||||
|  | ||||
| List fields | ||||
| ----------- | ||||
| @@ -121,7 +130,7 @@ Embedded documents | ||||
| MongoDB has the ability to embed documents within other documents. Schemata may | ||||
| be defined for these embedded documents, just as they may be for regular | ||||
| documents. To create an embedded document, just define a document as usual, but | ||||
| inherit from :class:`~mongoengine.EmbeddedDocument` rather than  | ||||
| inherit from :class:`~mongoengine.EmbeddedDocument` rather than | ||||
| :class:`~mongoengine.Document`:: | ||||
|  | ||||
|     class Comment(EmbeddedDocument): | ||||
| @@ -144,7 +153,7 @@ Often, an embedded document may be used instead of a dictionary -- generally | ||||
| this is recommended as dictionaries don't support validation or custom field | ||||
| types. However, sometimes you will not know the structure of what you want to | ||||
| store; in this situation a :class:`~mongoengine.DictField` is appropriate:: | ||||
|      | ||||
|  | ||||
|     class SurveyResponse(Document): | ||||
|         date = DateTimeField() | ||||
|         user = ReferenceField(User) | ||||
| @@ -152,16 +161,19 @@ store; in this situation a :class:`~mongoengine.DictField` is appropriate:: | ||||
|  | ||||
|     survey_response = SurveyResponse(date=datetime.now(), user=request.user) | ||||
|     response_form = ResponseForm(request.POST) | ||||
|     survey_response.answers = response_form.cleaned_data()    | ||||
|     survey_response.answers = response_form.cleaned_data() | ||||
|     survey_response.save() | ||||
|  | ||||
| Dictionaries can store complex data, other dictionaries, lists, references to | ||||
| other objects, so are the most flexible field type available. | ||||
|  | ||||
| Reference fields | ||||
| ---------------- | ||||
| References may be stored to other documents in the database using the | ||||
| :class:`~mongoengine.ReferenceField`. Pass in another document class as the | ||||
| first argument to the constructor, then simply assign document objects to the | ||||
| field:: | ||||
|      | ||||
|  | ||||
|     class User(Document): | ||||
|         name = StringField() | ||||
|  | ||||
| @@ -235,13 +247,13 @@ Its value can take any of the following constants: | ||||
|    in-memory, by the MongoEngine module, it is of the upmost importance | ||||
|    that the module that declares the relationship is loaded **BEFORE** the | ||||
|    delete is invoked. | ||||
|     | ||||
|  | ||||
|    If, for example, the :class:`Employee` object lives in the | ||||
|    :mod:`payroll` app, and the :class:`ProfilePage` in the :mod:`people` | ||||
|    app, it is extremely important that the :mod:`people` app is loaded | ||||
|    before any employee is removed, because otherwise, MongoEngine could | ||||
|    never know this relationship exists. | ||||
|     | ||||
|  | ||||
|    In Django, be sure to put all apps that have such delete rule declarations in | ||||
|    their :file:`models.py` in the :const:`INSTALLED_APPS` tuple. | ||||
|  | ||||
| @@ -250,15 +262,15 @@ Generic reference fields | ||||
| '''''''''''''''''''''''' | ||||
| A second kind of reference field also exists, | ||||
| :class:`~mongoengine.GenericReferenceField`. This allows you to reference any | ||||
| kind of :class:`~mongoengine.Document`, and hence doesn't take a  | ||||
| kind of :class:`~mongoengine.Document`, and hence doesn't take a | ||||
| :class:`~mongoengine.Document` subclass as a constructor argument:: | ||||
|  | ||||
|     class Link(Document): | ||||
|         url = StringField() | ||||
|          | ||||
|  | ||||
|     class Post(Document): | ||||
|         title = StringField() | ||||
|          | ||||
|  | ||||
|     class Bookmark(Document): | ||||
|         bookmark_object = GenericReferenceField() | ||||
|  | ||||
| @@ -272,9 +284,10 @@ kind of :class:`~mongoengine.Document`, and hence doesn't take a | ||||
|     Bookmark(bookmark_object=post).save() | ||||
|  | ||||
| .. note:: | ||||
|  | ||||
|    Using :class:`~mongoengine.GenericReferenceField`\ s is slightly less | ||||
|    efficient than the standard :class:`~mongoengine.ReferenceField`\ s, so if | ||||
|    you will only be referencing one document type, prefer the standard  | ||||
|    you will only be referencing one document type, prefer the standard | ||||
|    :class:`~mongoengine.ReferenceField`. | ||||
|  | ||||
| Uniqueness constraints | ||||
| @@ -282,7 +295,7 @@ Uniqueness constraints | ||||
| MongoEngine allows you to specify that a field should be unique across a | ||||
| collection by providing ``unique=True`` to a :class:`~mongoengine.Field`\ 's | ||||
| constructor. If you try to save a document that has the same value for a unique | ||||
| field as a document that is already in the database, a  | ||||
| field as a document that is already in the database, a | ||||
| :class:`~mongoengine.OperationError` will be raised. You may also specify | ||||
| multi-field uniqueness constraints by using :attr:`unique_with`, which may be | ||||
| either a single field name, or a list or tuple of field names:: | ||||
| @@ -294,14 +307,14 @@ either a single field name, or a list or tuple of field names:: | ||||
|  | ||||
| Skipping Document validation on save | ||||
| ------------------------------------ | ||||
| You can also skip the whole document validation process by setting  | ||||
| ``validate=False`` when caling the :meth:`~mongoengine.document.Document.save`  | ||||
| You can also skip the whole document validation process by setting | ||||
| ``validate=False`` when caling the :meth:`~mongoengine.document.Document.save` | ||||
| method:: | ||||
|  | ||||
|     class Recipient(Document): | ||||
|         name = StringField() | ||||
|         email = EmailField() | ||||
|      | ||||
|  | ||||
|     recipient = Recipient(name='admin', email='root@localhost') | ||||
|     recipient.save()               # will raise a ValidationError while | ||||
|     recipient.save(validate=False) # won't | ||||
| @@ -329,7 +342,7 @@ A :class:`~mongoengine.Document` may use a **Capped Collection** by specifying | ||||
| stored in the collection, and :attr:`max_size` is the maximum size of the | ||||
| collection in bytes. If :attr:`max_size` is not specified and | ||||
| :attr:`max_documents` is, :attr:`max_size` defaults to 10000000 bytes (10MB). | ||||
| The following example shows a :class:`Log` document that will be limited to  | ||||
| The following example shows a :class:`Log` document that will be limited to | ||||
| 1000 entries and 2MB of disk space:: | ||||
|  | ||||
|     class Log(Document): | ||||
| @@ -369,9 +382,10 @@ If a dictionary is passed then the following options are available: | ||||
|     Whether the index should be sparse. | ||||
|  | ||||
| .. note:: | ||||
|    Geospatial indexes will be automatically created for all  | ||||
|  | ||||
|    Geospatial indexes will be automatically created for all | ||||
|    :class:`~mongoengine.GeoPointField`\ s | ||||
|          | ||||
|  | ||||
| Ordering | ||||
| ======== | ||||
| A default ordering can be specified for your | ||||
| @@ -393,7 +407,7 @@ subsequent calls to :meth:`~mongoengine.queryset.QuerySet.order_by`. :: | ||||
|     blog_post_1 = BlogPost(title="Blog Post #1") | ||||
|     blog_post_1.published_date = datetime(2010, 1, 5, 0, 0 ,0) | ||||
|  | ||||
|     blog_post_2 = BlogPost(title="Blog Post #2")  | ||||
|     blog_post_2 = BlogPost(title="Blog Post #2") | ||||
|     blog_post_2.published_date = datetime(2010, 1, 6, 0, 0 ,0) | ||||
|  | ||||
|     blog_post_3 = BlogPost(title="Blog Post #3") | ||||
| @@ -405,7 +419,7 @@ subsequent calls to :meth:`~mongoengine.queryset.QuerySet.order_by`. :: | ||||
|  | ||||
|     # get the "first" BlogPost using default ordering | ||||
|     # from BlogPost.meta.ordering | ||||
|     latest_post = BlogPost.objects.first()  | ||||
|     latest_post = BlogPost.objects.first() | ||||
|     assert latest_post.title == "Blog Post #3" | ||||
|  | ||||
|     # override default ordering, order BlogPosts by "published_date" | ||||
| @@ -434,7 +448,7 @@ Working with existing data | ||||
| To enable correct retrieval of documents involved in this kind of heirarchy, | ||||
| two extra attributes are stored on each document in the database: :attr:`_cls` | ||||
| and :attr:`_types`. These are hidden from the user through the MongoEngine | ||||
| interface, but may not be present if you are trying to use MongoEngine with  | ||||
| interface, but may not be present if you are trying to use MongoEngine with | ||||
| an existing database. For this reason, you may disable this inheritance | ||||
| mechansim, removing the dependency of :attr:`_cls` and :attr:`_types`, enabling | ||||
| you to work with existing databases. To disable inheritance on a document | ||||
|   | ||||
		Reference in New Issue
	
	Block a user