|
|
|
@@ -2,19 +2,21 @@
|
|
|
|
Using MongoEngine with Django
|
|
|
|
Using MongoEngine with Django
|
|
|
|
=============================
|
|
|
|
=============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. note :: Updated to support Django 1.4
|
|
|
|
|
|
|
|
|
|
|
|
Connecting
|
|
|
|
Connecting
|
|
|
|
==========
|
|
|
|
==========
|
|
|
|
In your **settings.py** file, ignore the standard database settings (unless you
|
|
|
|
In your **settings.py** file, ignore the standard database settings (unless you
|
|
|
|
also plan to use the ORM in your project), and instead call
|
|
|
|
also plan to use the ORM in your project), and instead call
|
|
|
|
:func:`~mongoengine.connect` somewhere in the settings module.
|
|
|
|
:func:`~mongoengine.connect` somewhere in the settings module.
|
|
|
|
|
|
|
|
|
|
|
|
Authentication
|
|
|
|
Authentication
|
|
|
|
==============
|
|
|
|
==============
|
|
|
|
MongoEngine includes a Django authentication backend, which uses MongoDB. The
|
|
|
|
MongoEngine includes a Django authentication backend, which uses MongoDB. The
|
|
|
|
:class:`~mongoengine.django.auth.User` model is a MongoEngine
|
|
|
|
:class:`~mongoengine.django.auth.User` model is a MongoEngine
|
|
|
|
:class:`~mongoengine.Document`, but implements most of the methods and
|
|
|
|
:class:`~mongoengine.Document`, but implements most of the methods and
|
|
|
|
attributes that the standard Django :class:`User` model does - so the two are
|
|
|
|
attributes that the standard Django :class:`User` model does - so the two are
|
|
|
|
moderately compatible. Using this backend will allow you to store users in
|
|
|
|
moderately compatible. Using this backend will allow you to store users in
|
|
|
|
MongoDB but still use many of the Django authentication infrastucture (such as
|
|
|
|
MongoDB but still use many of the Django authentication infrastucture (such as
|
|
|
|
the :func:`login_required` decorator and the :func:`authenticate` function). To
|
|
|
|
the :func:`login_required` decorator and the :func:`authenticate` function). To
|
|
|
|
enable the MongoEngine auth backend, add the following to you **settings.py**
|
|
|
|
enable the MongoEngine auth backend, add the following to you **settings.py**
|
|
|
|
@@ -24,7 +26,7 @@ file::
|
|
|
|
'mongoengine.django.auth.MongoEngineBackend',
|
|
|
|
'mongoengine.django.auth.MongoEngineBackend',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
The :mod:`~mongoengine.django.auth` module also contains a
|
|
|
|
The :mod:`~mongoengine.django.auth` module also contains a
|
|
|
|
:func:`~mongoengine.django.auth.get_user` helper function, that takes a user's
|
|
|
|
:func:`~mongoengine.django.auth.get_user` helper function, that takes a user's
|
|
|
|
:attr:`id` and returns a :class:`~mongoengine.django.auth.User` object.
|
|
|
|
:attr:`id` and returns a :class:`~mongoengine.django.auth.User` object.
|
|
|
|
|
|
|
|
|
|
|
|
@@ -49,9 +51,9 @@ Storage
|
|
|
|
=======
|
|
|
|
=======
|
|
|
|
With MongoEngine's support for GridFS via the :class:`~mongoengine.FileField`,
|
|
|
|
With MongoEngine's support for GridFS via the :class:`~mongoengine.FileField`,
|
|
|
|
it is useful to have a Django file storage backend that wraps this. The new
|
|
|
|
it is useful to have a Django file storage backend that wraps this. The new
|
|
|
|
storage module is called :class:`~mongoengine.django.storage.GridFSStorage`.
|
|
|
|
storage module is called :class:`~mongoengine.django.storage.GridFSStorage`.
|
|
|
|
Using it is very similar to using the default FileSystemStorage.::
|
|
|
|
Using it is very similar to using the default FileSystemStorage.::
|
|
|
|
|
|
|
|
|
|
|
|
from mongoengine.django.storage import GridFSStorage
|
|
|
|
from mongoengine.django.storage import GridFSStorage
|
|
|
|
fs = GridFSStorage()
|
|
|
|
fs = GridFSStorage()
|
|
|
|
|
|
|
|
|
|
|
|
|