First, the Environment Basic step 1, enter the development environment of the virtual space, do not know see the transmission Door 2, the version of the basic package
django@1.11.8 mongoengine@0.15.0
3. Installation package
Pip Install Mongoengine
4, create a new Django project, and specify the Python.exe in the virtual space Two, configure in Django
1, in the settings.py to carry out the basic configuration
DATABASES = {'
default ': {
' ENGINE ': None, # Connect default database to None
}
} from
mongoengine import Connect
Connect (' Test ') # Connection database name
2. Create a new app
3. Create a new data model in the models.py of a new app
Import Mongoengine
class Studentmodel (Mongoengine. Document):
name = Mongoengine. Stringfield (max_length=16) Age
= Mongoengine. Intfield (default=0)
4. Create a view in the view file
From django.shortcuts import Render, HttpResponse
# Create your views here.
#. Represents the models from. Models import Studentmodel under the current package from
django.views.generic import View
class Student (View ):
def get (self, request):
StudentModel.objects.create (name= ' water mark ', age=) return
httpresponse (' Hello Word ')
5. Configure URL
From django.conf.urls import URL from
django.contrib import admin from
student.views Import student
Urlpatterns = [
url (R ' ^admin/', admin.site.urls),
url (r ' ^student/$ ', Student.as_view (), name= ' student ')
]
Third, on Add and delete to check 1, increase data
2, the query data (return is a queryset)
Class Student (View):
def get (self, request): Result
= StudentModel.objects.filter (name= ' water mark ')
print ( Result[0].age) return
httpresponse (' Hello word ')
3, modify the data
Class Student (View):
def get (self, request): Result
= StudentModel.objects.filter (name= ' water mark '). (Name= ' John ')
Print (Result) return
httpresponse (' Hello word ')
4. Delete data
Class Student (View):
def get (self, request): Result
= StudentModel.objects.filter (name= ' John '). ()
print [result] return
httpresponse (' Hello word ')
Iv. more on Reference documentation 1, Official document 2, reference to MySQL operations 3, MongoDB articles