Django笔记二 使用初步
2015-01-08 22:50
281 查看
一,Django ORM
建立一个简单博客,定义了topical和article 两个类,两个类都从models.MOdel 继承 变量类型都由models中定义,还可以设置外键,等关系。具体一对多,多对多是怎么实现稍后再详细研究。
是在执行上面的更改,所以每次对models.py 文件的修改都要执行以上两个命令,英文文档是这样总结的。三个步骤。
Migrations are very powerful and let you change your models over time, as you develop your project, without the need to delete your database or tables and make new ones - it specializes in upgrading your database live, without losing data. We’ll cover them
in more depth in a later part of the tutorial, but for now, remember the three-step guide to making model changes:
Change your models (in models.py).
Run python manage.py makemigrations to
create migrations for those changes
Run python manage.py migrate to
apply those changes to the database.
使用manage.py shell .可以对数据库中的model进行一些操作
Article.objects.all() 查询所有的article
不像java,这里的model没有定义构造函数,跟hibernate一样简单的操作不需要sql,直接就能搞定。但是能不能,怎么执行sql,还是个问题?
二,建立admin来管理
django 提供了一个方便的后台,
只要执行 python manage,py createsuperuser 就能设置用户名,密码等一些信息。在地址栏输入http://localhost:8000/blog/ 就能进入登录界面,输入账号密码,就能登录,来管理刚刚建立的在数据库中的model. 支持 修改成各种格式。
建立一个简单博客,定义了topical和article 两个类,两个类都从models.MOdel 继承 变量类型都由models中定义,还可以设置外键,等关系。具体一对多,多对多是怎么实现稍后再详细研究。
from django.db import models # Create your models here. class Topical(models.Model): name=models.CharField(max_length=30) def __str__(self): return self.name class Article(models.Model): topical=models.ForeignKey(Topical) title=models.CharField(max_length=200) content=models.TextField() pub_date=models.DateTimeField('date published') def __str__(self): return self.title切换到应用目录下执行python manage,py makemigrate blog (the name of app) 官方文档说这是在告诉django 你已经对model做了修改,
python manage.py migrate
是在执行上面的更改,所以每次对models.py 文件的修改都要执行以上两个命令,英文文档是这样总结的。三个步骤。
Migrations are very powerful and let you change your models over time, as you develop your project, without the need to delete your database or tables and make new ones - it specializes in upgrading your database live, without losing data. We’ll cover them
in more depth in a later part of the tutorial, but for now, remember the three-step guide to making model changes:
Change your models (in models.py).
Run python manage.py makemigrations to
create migrations for those changes
Run python manage.py migrate to
apply those changes to the database.
使用manage.py shell .可以对数据库中的model进行一些操作
Article.objects.all() 查询所有的article
不像java,这里的model没有定义构造函数,跟hibernate一样简单的操作不需要sql,直接就能搞定。但是能不能,怎么执行sql,还是个问题?
二,建立admin来管理
django 提供了一个方便的后台,
只要执行 python manage,py createsuperuser 就能设置用户名,密码等一些信息。在地址栏输入http://localhost:8000/blog/ 就能进入登录界面,输入账号密码,就能登录,来管理刚刚建立的在数据库中的model. 支持 修改成各种格式。
相关文章推荐
- Python Web开发 之Django框架入门学习笔记(一)——安装和初步使用
- Memcached学习笔记——windows上初步使用
- [Java Path Finder][JPF学习笔记][6]jpf-symbc的初步使用
- 初步使用OSC@Git (笔记)
- The Definitive Guide To Django 2 学习笔记(七) 第四章 模板 (三)使用模板系统
- The Definitive Guide To Django 2 学习笔记(六) 第四章 模板 (二)使用模板系统
- Pyhton_Django学习笔记(五) 模型与MYSQL数据库使用
- Django学习笔记(二)Django使用template
- Crushe Django 入门笔记4 使用模型
- Crusher Django 学习笔记4 使用Model
- Django使用MySQL笔记
- Memcached学习笔记——windows上初步使用(与java结合)
- php学习笔记(三十四)smarty框架的初步使用和注意事项
- php学习笔记(二十六)php中session的初步使用(基于cookie的)
- Django 入门笔记5 使用admin 管理
- [Java Path Finder][JPF学习笔记][6]jpf-symbc的初步使用
- php学习笔记(二十二)PDO的安装和初步使用
- Django学习笔记(7)---多数据库中跨数据库使用多对多
- python Django 学习笔记(三)—— 模版的使用
- Cocos2dx 3.1.1 学习笔记整理(4):事件监听与Action的初步使用