Django 连接数据库

1、Django默认是用SQLite3数据库
2、使用Navicat打开SQLite3数据库
连接 --> 选择SQLite -> 创建一个连接名字->选择类型是打开还是新建 - >如果是打开需要选择打开的db文件

 


3、创建模型同时生成对应的表
a) 需求:创建一个问题(Question)对应多个选项(Choice)
b) 创建Model,在应用下的models.py中编写Question类和Choice类
from django.db import models

# Create your models here.


# 创建一个Question类- 对应的是数据库的表名
class Question(models.Model):
# 创建一个question_text字段,是varchar(200)
question_text = models.CharField(max_length=200)
# 创建一个pub_date字段,是datetime类型
pub_date = models.DateTimeField('date published')


# 创建一个Choice类- 对应的是数据库的表名,Question和Choice是一对多的关系
class Choice(models.Model):
# 创建一个外键关联Question表
question = models.ForeignKey(Question, on_delete=models.CASCADE)
# 创建选项内容,是varchar类型长度是200
choice_text = models.CharField(max_length=200)
# 创建投票数量字段,是int类型,默认是0
votes = models.IntegerField(default=0)

c) 将polls应用安装到项目中
在项目的包下的settings.py中的INSTALLED_APPS中添加应用
INSTALLED_APPS = [
# 以应用的名字
#'polls',
# 以应用的配置类名-推荐
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]


d) 生成数据库的迁移文件, 执行完后会在app下的migrations下生成迁移文件0001_initial.py文件
python mange.py makemigrations polls
e) 查看迁移过程
python manage.py sqlmigrate polls 0001
f) 执行数据库的迁移(相当于在数据库中建表)
python manage.py migrate polls

到此会发在数据库中存在polls_question, polls_choice两个表

 


4、使用Django的ORM框架进行数据库的操作
ORM:关系映射模型
a) 使用python manage.py shell
b) 导包 from polls.models import Question, Choice
c) 查询:
1) 获取所有记录,返回一个集合
Question.objects.all()
2) 保存记录
先创建对象,在执行保存方法
from django.utils import timezone
q = Question(question_text = 'What\' this?', pub_date=timezone.now())
q.save() # 保存到数据库
3) 修改记录:
q.question_text = '这是什么呢?'
q.save()

4) 可以通过重写__str__()方法来展示查询的结果
Question.objects.all()
5) 在类中自定义方法was_published_recently(self),判断此问题是否是最近一天发布的
a) 通过q = Question.objects.get(pk=1) 返回的是对象,如果对象不存在就会提示错误
q.was_published_recently()
6) filter() 方法进行过滤查询
a) Question.objects.filter(id=1) 返回的是一个集合,如果结果不存在,返回空集合
select * from polls_question where id = 1
b) Question.objects.filter(pub_date__year=timezone.now().year)返回的是一个集合,如果结果不存在,返回空集合
select * from polls_question where year(pub_date) = year(now())
c) Question.objects.filter(question_text__startswith='这是'), 查询question_text以'这是开头的记录'
select * from polls_question where question_text like '这是%'

d) 一对多关联查询
Question和Choice是一对多的关系,那么在Question应该Choice对象的集合,在Choice对象中有Question对象
a) 先获取Question中id=1的记录,在获取这个记录下的Choice集合
q = Question.objects.get(id=1)
q.choice_set.all() #返回所有记录,如果没有就是空集合
q.choice_set.count() #返回记录数
b) 创建多方记录
q.choice_set.create(choice_text='Not much', votes=0)
q.choice_set.create(choice_text='The sky', votes=0)
c = q.choice_set.create(choice_text='Just hacking again', votes=0) 其中返回一个Choice对象
c) 获取多方中的Question对象
q = c.question 就会返回一个Question对象

d) 查询近一年的问题的选项
cs = Choice.objects.filter(question__pub_date__year = timezone.year)

f) 获取id为1的问题下的以just hacking开头选项
q = Question.objects.get(id=1)
c = q.choice_set.filter(choice_text__startswith = 'just hacking')

g) 删除, 物理删除
c.delete()

posted @ 2019-03-07 12:15  一念之间的抉择  阅读(304)  评论(0编辑  收藏  举报