Django MongoDB Django NoSQL Solution

Source: Internet
Author: User

1. Install MongoDB

Mongodb home http://www.mongodb.org/

1, download the required version, address: http://www.mongodb.org/display/DOCS/Downloads

(Take windows as an example, linux should be relatively simple), can refer to the official document: http://www.mongodb.org/display/DOCS/Home

2. Add MongoDB to the Environment Variable

3. Set the path for storing database files, such as d: \ db.

4. Open CMD. Do not close this

Mongod-- Dbpath=D: \ Db \ data

5. Start mongo.

  1. MongoDB shell version: 1.4.0
  2. Url: test
  3. Connecting to: test
  4. Type "exit" to exit
  5. Type "help" for help

If this occurs, it indicates the operation is successful.

Do not close this.

STEP 2 Django MongoDB

Official documents for reference: http://django-mongodb.org/topics/setup.html

We recommend using virtualenv to build a virtual development environment. (A python environment with independent directories will be created)

Another reason is that Django-nonrel is used.

Pip install virtualenv

Create Environment

Virtualenv myproject

For linux,

Source myproject/bin/activate

For windows, you need to enable Scripts to run and then activate

Install django-nonrel

Pip install hg + https://bitbucket.org/wkornewald/django-nonrel

Install djangotoolbox

Pip install hg + https://bitbucket.org/wkornewald/djangotoolbox

Install mongodb-engine

Pip install git + https://github.com/django-nonrel/mongodb-engine

For windows, you can directly consider (gitmecurial needs to be installed) and open the three webpages, download the files, install

Python steup. py install

After installation, add djangotoolbox to settings. py.

Modify settings. py

  1. DATABASES= {
  2. 'Default ':{
  3. 'Engine': 'django _ mongodb_engine ',
  4. 'Name': 'My _ database'
  5. }
  6. }

More settings: http://django-mongodb.org/reference/settings.html

And

Manage. py syncdb

STEP 3 Test

Documents for reference: http://cn.docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-django-mongodb-engine/

Create a new app and modify models. py.

  1. From django. db import models
  2. From django. core. urlresolvers import reverse
  3. From djangotoolbox. fields import ListField, EmbeddedModelField
  4. Class Post (models. Model ):
  5. Created_at=Models. DateTimeField (Auto_now_add=True,Db_index=True)
  6. Title=Models. CharField (Max_length=255)
  7. Slug=Models. SlugField ()
  8. Body=Models. TextField ()
  9. Comments=ListField(EmbeddedModelField ('comment '),Editable=False)
  10. Def get_absolute_url (self ):
  11. Return reverse ('post ',Kwargs= {"Slug": self. slug })
  12. Def _ unicode _ (self ):
  13. Return self. title
  14. Class Meta:
  15. Ordering= ["-Created_at"]
  16. Class Comment (models. Model ):
  17. Created_at=Models. DateTimeField (Auto_now_add=True)
  18. Body=Models. TextField (Verbose_name="Comment")
  19. Author=Models. CharField (Verbose_name="Name",Max_length=255)

Add data

Manage. py shell

Create post

  1. >>>FromTumblelog. modelsImport*
  2. >>> Post = Post (
  3. ... Title ="Hello World! ",
  4. ... Slug ="Hello-world",
  5. ... Body ="Welcome to my new shiny Tumble log powered by MongoDB and Django-MongoDB! "
  6. ...)
  7. >>> Post. save ()

Create comments

  1. >>>Post. comments
  2. []
  3. >>> Comment=Comment(
  4. ...Author="Joe Bloggs",
  5. ...Body="Great post! I'm looking forward to reading your blog")
  6. >>>Post. comments. append (comment)
  7. >>>Post. save ()

Check Post

  1. >>> Post = Post. objects. get ()
  2. >>> Post
  3. <Post: Hello World!>
  4. >>> Post. comments
  5. [<Comment: Comment object>] </

Conclusion: I still don't want to use it, although I always want to know the principle. But there is a gap with the imagination.

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.