django使用记录
安装virtualenv
easy_install virtualenv
生成虚拟环境
virtualenv myenv
进入虚拟环境
cd myenv
. bin/activate
下载安装django
easy_install django
生成project
django-admin.py startproject mysite
运行开发服务器
cd mysite
远程访问
python manage.py runserver 0.0.0.0:8000
浏览器访问:http://xxx.xxx.xxx.xxx:8000
本地访问
python manage.py runserver
浏览器访问http://127.0.0.1:8000/
在settings.py中设置数据库信息,
修改DATABASES中的参数
同步表结构信息
python manage.py syncdb
注:需要先安装MySQLdb,不然会出错
生成app,(多个app可组成一个project)
python manage.py startapp myapp
在setttings.py中添加myapp
生成myapp中models中定义的表结构
python manage.py sql myapp
python manage.py syncdb
进入shell模式检视models
python manage.py shell
离开虚拟环境
deactivate
modle 特点
Forward access to one-to-many relationships is cached the first time the related object is accessed. Subsequent accesses to the foreign key on the same object instance are cached. Example:
>>> e = Entry.objects.get(id=2)
>>> print e.blog # Hits the database to retrieve the associated Blog.
>>> print e.blog # Doesn't hit the database; uses cached version.
Aggregation
主要是为了实现group by的效果
http://fcamel-life.blogspot.com/2010/04/django-group-by.html
浙公网安备 33010602011771号