Django MongoDB Django NoSQL方案
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
1 安装MongoDB
linux的话,
windows需要打开Scripts 运行,再activate
更多的设置:http://django-mongodb.org/reference/settings.html
添加数据
创建评论
检查帖子
mongodb 主页 http://www.mongodb.org/
1,下载所需要的版本,地址:http://www.mongodb.org/display/DOCS/Downloads
(以windows为例,linux下应该比较简单),可以参照官方文档:http://www.mongodb.org/display/DOCS/Home
2,将MongoDB加入环境变量
3,设定好放置数据库文件的路径,比如d:\db
4,打开CMD,不要关闭这个
mongod --dbpath=d:\db\data
MongoDB shell version: 1.4.0
url: test
connecting to: test
type "exit" to exit
type "help" for help
>
如果出现这个就表示成功
不要关闭这个。
STEP 2 Django MongoDB
可参考的官方文档:http://django-mongodb.org/topics/setup.html
官方建议使用virtualenv,可以构建虚拟开发环境。(会创建独立目录的python环境)
另一原因,使用的是Django-nonrel。
pip install virtualenv
virtualenv myproject
linux的话,
source myproject/bin/activate
windows需要打开Scripts 运行,再activate
安装django-nonrel
pip install hg+https://bitbucket.org/wkornewald/django-nonrel
安装djangotoolboxpip install hg+https://bitbucket.org/wkornewald/djangotoolbox
安装mongodb-engine
pip install git+https://github.com/django-nonrel/mongodb-engine
python steup.py install
安装完后,需要将djangotoolbox加入settings.py
修改settings.py
DATABASES = {
'default' : {
'ENGINE' : 'django_mongodb_engine',
'NAME' : 'my_database'
}
}
更多的设置:http://django-mongodb.org/reference/settings.html
以及
manage.py syncdb
STEP 3 测试
可参考的中文文档:http://cn.docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-django-mongodb-engine/
创建一个新的app,然后修改models.py
from django.db import models
from django.core.urlresolvers import reverse
from djangotoolbox.fields import ListField, EmbeddedModelField
class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
title = models.CharField(max_length=255)
slug = models.SlugField()
body = models.TextField()
comments = ListField(EmbeddedModelField('Comment'), editable=False)
def get_absolute_url(self):
return reverse('post', kwargs={"slug": self.slug})
def __unicode__(self):
return self.title
class Meta:
ordering = ["-created_at"]
class Comment(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
body = models.TextField(verbose_name="Comment")
author = models.CharField(verbose_name="Name", max_length=255)
添加数据
manage.py shell
>>> from tumblelog.models import *
>>> post = Post(
... title="Hello World!",
... slug="hello-world",
... body = "Welcome to my new shiny Tumble log powered by MongoDB and Django-MongoDB!"
... )
>>> post.save()
创建评论
>>> post.comments
[]
>>> comment = Comment(
... author="Joe Bloggs",
... body="Great post! I'm looking forward to reading your blog")
>>> post.comments.append(comment)
>>> post.save()
检查帖子
>>> post = Post.objects.get()
>>> post
<Post: Hello World!>
>>> post.comments
[<Comment: Comment object>]
总结:还是不想用,虽然一直想知道原理。但是和想象中的有差距。
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献20条内容
所有评论(0)