控制台输入
python manage.py shell进入虚拟环境
>>>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()
>>> 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)
查询时支持用pk代表主键名称 Topic.objects.get(pk=1)效果一样的
>>> 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)
>>> 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
>>> 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
>>> 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 |
>>> Topic.objects.exclude(id__lte=3)
<QuerySet [<Topic: 4:four topic>]>
>>> 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
>>> 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
>>> 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
(未完,,,,,)
原文:https://www.cnblogs.com/xymaxbf/p/11919808.html