Django Connection MongoDB Database _mongodb

Source: Internet
Author: User
Tags mongodb
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.