Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to retrieve data from mongodb database using django?

i am new to mongodb and django . i setup all the models and urls.py in django. but its time to retrieve the information of database on view page. my database looks like:

{
"_id": {
    "$oid": "52221778633a610c58c131e6"
},
"text": "just",
"tags": [
    "mongo",
    "django"
],
"comments": [],
"title": "hello" }

database name:events collection name:polls_post

my urls.py is look like:

from django.conf.urls.defaults import patterns, include, url
from django.views.generic import ListView, DetailView
from polls.models import Post

urlpatterns = patterns('',
     url(r'^time/$','polls.views.current_datetime'),
     url(r'^events/$','polls.views.events'),
)

my views.py looks like:

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

def events(request):
    html = "<html><body>title is:<h1></h1></body></html>" 
    return HttpResponse(html)    

so, how will we fetch text, title and comments from database so that it can be shown on web page ?

like image 738
arglee Avatar asked Sep 01 '13 10:09

arglee


People also ask

Can I use Django with MongoDB?

Django, the most popular Python web framework, is an ideal tool to build secure and easy-to-maintain applications using MongoDB. Using MongoDB with Django is advantageous because: Every second, more and more unstructured data is generated from various sources like chats, real-time streams, feeds, and surveys.

How recover data from MongoDB?

You can use read operations to retrieve data from your MongoDB database. There are multiple types of read operations that access the data in different ways. If you want to request results based on a set of criteria from the existing set of data, you can use a find operation such as the find() or findOne() methods.


1 Answers

To have access to database, django has something called models. This is a full fletched ORM to abstract nuts and bolts of underlying database. Django's ORM is fully functional for Relational DBs. Mongo being NoSQL, we have to look for other options. The most widely used ORM's for MongoDB include Mongokit and MongoEngine. These are the wrappers built on top of Pymongo. For simplicity, here is a short code snippet for fetching data from mongoDB using pymongo.

from pymongo import Connection

server="localhost"
port = 27017
#Establish a connection with mongo instance.
conn = Connection(server,port)

To get a single document from collection, use find_one.

poll = conn.events.polls_post.find_one({},{"title" : 1}) #first parameter is the query, second one is the projection.
print "Title : ", poll['title']

To get all documents from collection, use find.

polls = conn.events.polls_post.find({},{"title" : 1})#first parameter is the query, second one is the projection.
for poll in polls:
    print "Title : ",poll['title']

If you would like to use Django's Non Relational Engine, you can write:

from models import Post 
posts = Post.objects.all() 
for post in posts: 
    print post.title

This assumes you have already created a model class in models.py: Something like this:

class Post(models.Model):
    created_on = models.DateTimeField(auto_now_add=True, null=True)
    title = models.CharField()
    text = models.TextField()
    tags = ListField()

For more useful stuff, checkout: pymongo and MognoDB. To use Django's Non relational Engine checkout : Django Non Relational Engine

Thanks!

like image 133
Rahul Tanwani Avatar answered Oct 21 '22 12:10

Rahul Tanwani