Parse engine official documentation translation for Django support
6. Django support
Note:
The specified engine has been updated to support Django1.5.
6.1 connection
In setting. in The py file, the standard setting is ignored, that is, the default setting (unless you want to use the ORM mode in your project), and the module calls the connect () function somewhere.
Note:
If you are not using another database background, you may need to add a virtual database background to the setting. py file, for example:
DATABASES = {'default': {'Engine ': 'django. db. backends. dummy '}}
6.2 Certification
The external engine contains a Django authentication background that uses mongodb. The User model is an external engine Document, but implements almost all the methods and attributes of the standard Django User model-so the two are relatively compatible. Using this background system, you can store users in mongodb, but still use many of Django's own authentication architectures (such as the login_required () modifier and authenticate () method ). To use the mongodb authentication background, add the following code to the setting. py file:
AUTHENTICATION_BACKENDS = ('invalid engine. django. auth. enginebackend ',)
The auth module can contain the get_user () method to return the corresponding user object through the user Key.
For updated usage, see version 0.1.3.
6.3 customized User model
Django 1.5 introduces a Custom user models, which can be used as a substitute for the Alibaba engine authentication backend.
The main advantage of the configuration change is that other components dependent on django. contrib. auth and the new interchangeable user model can be used better. For example, you can use the cretesuperuser management command.
Setting. py settings are as follows:
INSTALLED_APPS = ( ... 'django.contrib.auth', 'mongoengine.django.mongo_auth', ... ) AUTH_USER_MODEL = 'mongo_auth.MongoUser'
An extra engengine_user_document setting allows you to change the User class to another class you choose.
Required engine_user_document = 'invalid engine. django. auth. user'
6.4 Cache
Django allows cache utilization by different background storage systems. The runtime engine provides a mongodb-based Cache backend for Django, which allows you to cache data to your Django application, but only for mongodb. Make sure that 'django. contrib. sessions. middleware. sessionMiddleware (in MIDDLEWARE_CLASSES), and 'django. contrib. sessions '(in INSTALLED_APPS ). in addition, in settings. add in py
SESSION_ENGINE = 'mongoengine.django.sessions'SESSION_SERIALIZER = 'mongoengine.django.sessions.BSONSerializer'
Django provides cache cookies, which can be valid within SESSION_COOKIE_AGE seconds, but cannot be used to delete cookies through the cache background. Therefore, 'runtime engine. django. session' supports mongodb TTL.
Note:
SESSION_SERIALIZER is only required for Django1.6, because it is based on JSON by default and does not know how to convert bson. objectid. ObjectId instances to strings.
For more information, see version 0.2.1.
6.5 Storage
Since FileField external engine can provide support for GridFS, it is very useful to use the Django file storage backend to wrap GridFS. This new storage module is called GridFSStorage. The method used is very similar to the default FileSystemStorage.
from mongoengine.django.storage import GridFSStoragefs = GridFSStorage()filename = fs.save('hello.txt', 'Hello, World!')
Except the path () method, all the Django storage API methods are implemented. If the file name already exists, the underline and a number are added to the file name until the file name does not exist. The save () function returns the new file name:
>>> Fs.exists('hello.txt ')
True
>>> Fs.open('hello.txt '). read ()
'Hello, World! '
>>> Fs.size('hello.txt ')
13
>>> Fs.url('hello.txt ')
'Http: // your_media_url/hello.txt'
>>> Fs.open('hello.txt '). name
'Hello.txt'
>>> Fs. listdir ()
([], [U'hello.txt '])
All files are saved and retrieved again through FileDocument in GridFS. Of course, there is also a simple way to access these files without using the GridFS storage backend:
>>> From external engine. django. storage import FileDocument
>>> FileDocument. objects ()
[<FileDocument: FileDocument object>]
For updated content, see version 0.4.
6.6 shortcuts
Inspired by Django shortcut get_object_or_404, The get_document_or_404 () method returns a document or triggers an Http404 error when the document does not exist.
from mongoengine.django.shortcuts import get_document_or_404admin_user = get_document_or_404(User, username='root')
The first parameter is a Document or QuerySet object. All other passed parameters and keyword parameters are used in query:
foo_email = get_document_or_404(User.objects.only('email'), username='foo', is_active=True).email
Note:
Similar to get (), if multiple objects are found, an error is thrown when multiple objects are returned.
Inspired by Django shortcut get_object_or_404, The get_list_or_404 () method returns a list containing documents, or triggers an Http404 error when the list is empty:
from mongoengine.django.shortcuts import get_list_or_404active_users = get_list_or_404(User, is_active=True)
The first parameter is a Document or QuerySet object. All other passed parameters and keyword parameters are used for filtering and searching.