Djongo:django and MongoDB connectors

Source: Internet
Author: User
Tags mongodb


Using MongoDB as a back-end database in a Django project does not change the Django ORM framework. Implement the Django User management program to add and modify files in the MongoDB database.




Usage


1.pip Install Djongo



2. Add the following code to the setting.py under the Django project:


DATABASES ={

    ‘default‘:{
        ‘ENGINE:‘djongo‘,
        ‘NAME‘:‘your-db-name‘,
              }
           }


3. Run manage.py makemigrations, then run manage.py migrate (required for First data table creation in MongoDB)



4. Complete.
Here is still to recommend my own built Python Development Learning Group: 725479218, the group is learning Python development, if you are learning Python, small series welcome you to join, everyone is the software Development Party, Do not regularly share dry goods (only Python software development related), including my own 2018 of the latest Python advanced information and high-level development tutorials, welcome to advanced and into the small partners to deep python


Requirements:


Version 1.python3.6 and above



2.MongoDB 3.4 and later



3. If a nested query or subquery is used in the project database, such as:


inner_qs =Blog.objects.filter(name__contains=‘Ch‘).values(‘name‘)
entries =Entry.objects.filter(blog__name__in=inner_qs)


Requires MongoDB3.6 and above.


How does it work?


Djongo changes to the Django ORM Framework are very small, which means that there will be no unnecessary errors. It transforms the SQL string query into a mongodb file query. At this point, all Django-related functions, databases, and so on are working like this.



Django's Service module:


‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.sessions‘,


All the others support it.


Usage in Django


The Djongo Connector for MongoDB ensures that you can:--re-use the Django Database/orm framework--work with the original Django variables--Validate your code beforehand--tiny SQL Join operations



Refer to Integrated Django and MongoDB for a more detailed introduction. If your project uses a complex database, you can also get support from an expert.


Adding data files using the Django User Management module


For example, you want to use Django to create a blog platform with MongoDB as a back-end database. Define the blog data table under your blog project app/models.py file:






Complete the settings and then visit localhost:80000/admin/you will see the following results.






Querying embedded data fields



In the above example, we want to query all the names that begin with Beatles, and use the following query method:






Refer to Using MongoDB data fields in Django for more information.


Djongo Manager


The Djongo Manager extends the functionality of the Django Manager. It allows you to use all the Pymongo API commands. You can define a custom manager like the Djongo manager in the model.






The use of the manager is the same as the Djongo Manager: Post = Entry.objects.get (Pk=pkey) will get an object with a primary key pkey.


Direct operation to Pymongo


MongoDB has very powerful query commands, and Djongomanager allows you to fully use it.






You can directly use any of the Pymongo commands by prefixing the command name with the MONGO prefix. For example, use aggregate for Blogpage (Blogpage is saved as a data table in SQL and saved as a collection in MongoDB), and the function name becomes mongoaggregate. Insert a file directly in the model (not using. Save ()), using Mongoinsertone ().



Djongo:django and MongoDB connectors


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.