Chapter 5, Django database layer
来源:互联网 发布:节假日堵车数据 编辑:程序博客网 时间:2024/05/23 21:32
1. run postgresql on ubuntu: sudo -u postgre psql
then input password
if you didn't set up user name, password or create db then follow the steps below:
sudo -u postgres createuser --superuser guangyi
then input the user name, like here, guangyi
sudo -u postgres psql
postgres=# \password guangyi
after inputting the new password
input:
sudo -u postgres createdb guangyi
input:
psql
then you should see something like this:
psql(9.1.9)
try "help" for help
guangyi=#
now we can write some SQL code to manage database, like
create database mydb;
so here we go. After doing all the steps above, we should have already set up the database.
2. edit django settings.py file, database section. Need to edit "ENGINE", "NAME","USER", "PASSWORD" 4 section.
3. test configuration: run python manage.py shell
when get into interpreter:
if nothing comes out after running those code, it means the connection is correct.
part2: First model
1. create a new app:
After that it will create a books directory with 4 python files in it. one of those 4 files is models.py
2. edit models
Basic data access:
When calling "save()", django then save the data into database.the second way to do this is using objects.create():
Adding Model String Representations
Make sure any model you define has a __unicode__() method
Inserting and Updating Data
Filtering data:
You can pass multiple argument into filter()it's similar to:
The filter() examples above all returned a QuerySet, which you can treat like a listThe get() example are different:
it returns and can only return 1 object, if it get more than 2 object, there will arise an exception: if the object does not exit:The DoesNotExist exception is an attribute of the model’s class – Publisher.DoesNotExist. In your applications, you’ll want to trap these exceptions, like this:
Ordering Data:
Publisher.objects.order_by()Django can let us specify a default order in models:
Slicing Data:
Updating Multiple Objects in One Statement: update()
save() method updates all the columnsuse .id to get an instance's id
The update() method works on any QuerySet, which means you can edit multiple records in bulk. Here’s how you might change the country from 'U.S.A.' to USA in each Publisher record:
The update() method has a return value – an integer representing how many records changed. In the above example, we got 2.
Deleting Objects: .delete()
But it’ll work if you add the all() method:
- Chapter 5, Django database layer
- !!!Chapter 5 The Network Layer
- Chapter 3. Interface Layer
- Database layer in Zend
- !!!Chapter 2 The Physical Layer
- !!!Chapter 6 The Transport Layer
- !!!Chapter 7 The Application Layer
- Zen Cart Database Abstraction Layer
- Drupal7 API Database abstraction layer
- Chapter 7Greenplum Database数据类型
- !!!Chapter 3 The Data Link Layer
- Chapter 14 The Block I/O Layer
- tango with django chapter 4 Django Basics
- Chapter 2:Installing the oracle database software
- Chapter 04–Creating a Database
- Chapter 1 Databases and Database Users
- Chapter 19 Configuring IPsec for Greenplum Database
- [Django] Base class in the model layer
- GitHub上最火的74个Android开源项目
- Q5+maxim dl
- Beijing 2008 / UVa 1428 / POJ 3928 / HDU 2492 Ping pong (树状数组)
- Knockout.js随手记(7)
- php+mysql+apache环境配置(我配过一遍成功)
- Chapter 5, Django database layer
- linux修改文件权限和用户组管理小结
- 联通宽带我世界改为使用路由器多台电脑上网的配置
- HighChartS cpu利用率动态图(Java版)
- 接口
- ubuntu 13.04 安装 JDK
- GoF设计模式学习-抽象工厂模式
- ubuntu中如何卸载软件
- python高效编程