4-数据库操作

输入数据

控制台输入

python manage.py shell进入虚拟环境

1、save插入

 
 
>>>from firstApp.models import Topic,Comment
>>>from django.contrib.auth.models import User #save插入方法
>>>user=User.objects.get(username='xym')
>>>topic=Topic(title='four topic',content='this is the four topic',user=user)
>>>topic.save()
>>>comment=Comment(content='so bad',up=123,down=321,topics_id=topic)
>>>comment.save()

2、create插入

>>> user1=User.objects.get(username='xym')
>>> topic_2=Topic.objects.create(title='second topic',content='this is the second topic',user=user1)
>>> coment=Comment.objects.create(content='so good!',up=99,down=33,topics=topic_2)

 

查询数据

1、get()查询:返回单实例对象

 查询时支持用pk代表主键名称 Topic.objects.get(pk=1)效果一样的

2、get_or_greate()查询:返回tuple对象。当有信息时,会返回查到的信息,并且返回false;没有信息时,则会创建信息,返回true(需要信息完整)

>>> Topic.objects.get_or_create(id=1,title='first topic')
(<Topic: 1:first topic>, False)
>>> Topic.objects.get_or_create(id=1)
(<Topic: 1:first topic>, False)
>>> Topic.objects.get_or_create(id=3)
(<Topic: 3:three>, False)

>>> Topic.objects.get_or_create(id=4,title='four topic',content='this is the four topic!',user=user)
(<Topic: 4:four topic>, True)

3、QuerySet的查询方法:返回可以是零个,一个或者多个

1、all()方法

>>> Topic.objects.all()
<QuerySet [<Topic: 4:four topic>, <Topic: 3:three>, <Topic: 2:second topic>, <Topic: 1:first topic>]>

>>> print(Topic.objects.all().query)#返回查询过程

SELECT `firstApp_topic`.`id`,
          `firstApp_topic`.`create_time`,
          `firstApp_topic`.`update_time`,
          `firstApp_topic`.`title`,
          `firstApp_topic`.`content`,
          `firstApp_topic`.`is_online`,
          `firstApp_topic`.`user_id`
FROM `firstApp_topic`
ORDER BY  `firstApp_topic`.`create_time` DESC

2、reverse方法:逆序打印

>>> Topic.objects.reverse()
<QuerySet [<Topic: 1:first topic>, <Topic: 2:second topic>, <Topic: 3:three>, <Topic: 4:four topic>]>


>>> print(Topic.objects.reverse().query) SELECT `firstApp_topic`.`id`, `firstApp_topic`.`create_time`, `firstApp_topic`.`update_time`, `firstApp_topic`.`title`, `firstApp_topic`.`content`, `firstApp_topic`.`is _online`, `firstApp_topic`.`user_id` FROM `firstApp_topic` ORDER BY `firstApp_topic`.`create_time` ASC

3、filter过滤方法:相当于sql语句中的where;连接符为双下划线

>>> Topic.objects.filter(id__lte=3)
<QuerySet [<Topic: 3:three>, <Topic: 2:second topic>, <Topic: 1:first topic>]>
查询关键字 含义 相当于where
gt   大于 >
gte 大于等于 >=
lt 小于 <
lte 小于等于 <=
exact 等于 =
iexact 忽略大小写的等于 like xyz
in 是否在集合中 in (a,b)
contains;icontains(忽略大小写) 是否包含 like binary %a%
startswith;istartswith(忽略大小写) 以,,,开头 like binary a%
endswith;iendswith(忽略大小写) 以,,,结尾 like binary %a

4、exclude反向过滤:

>>> Topic.objects.exclude(id__lte=3)
<QuerySet [<Topic: 4:four topic>]>

5、链式查询

>>> Topic.objects.filter(id__lte=3).exclude(title__contains='tle')
<QuerySet [<Topic: 3:three>, <Topic: 2:second topic>, <Topic: 1:first topic>]>
>>> Topic.objects.filter(id__lte=3).exclude(title__contains='tle').filter(content__contains='first')
<QuerySet [<Topic: 1:first topic>]>

查询过程

print(Topic.objects.filter(id__lte=3).exclude(title__contains='tle').filter(content__contains='first').query)

SELECT `firstApp_topic`.`id`,
         `firstApp_topic`.`create_time`,
         `firstApp_topic`.`update_time`,
         `firstApp_topic`.`title`,
         `firstApp_topic`.`content`,
         `firstApp_topic`.`is _online`,
         `firstApp_topic`.`user_id`
FROM `firstApp_topic`
WHERE (`firstApp_topic`.`id` <= 3
        AND NOT (`firstApp_topic`.`title` LIKE BINARY %tle%)
        AND `firstApp_topic`.` content` LIKE BINARY %first%)
ORDER BY  `firstApp_topic`.`create_time` DESC

6、value返回结果:返回指定字段字典数据

>>> Comment.objects.values('id','content')
<QuerySet [{'id': 3, 'content': 'very good!!'}, {'id': 2, 'content': 'good!'}, {'id': 1, 'content': 'very good!'}]>
SELECT `firstApp_comment`.`id`,
         `firstApp_comment`.`content`
FROM `firstApp_comment`
ORDER BY  `firstApp_comment`.`create_time` DESC

7、values_list:返回元组数据

>>> Comment.objects.values_list('id','content')
<QuerySet [(3, 'very good!!'), (2, 'good!'), (1, 'very good!')]>
SELECT `firstApp_comment`.`id`,
         `firstApp_comment`.`content`
FROM `firstApp_comment`
ORDER BY  `firstApp_comment`.`create_time` DESC

(未完,,,,,)

猜你喜欢

转载自www.cnblogs.com/xymaxbf/p/11919808.html
今日推荐