首页 > 其他 > 详细

django从0开始-02

时间:2020-04-11 00:54:05      阅读:65      评论:0      收藏:0      [点我收藏+]

django中sql语句的用法:

  

1.get
player = models.Player.objects.get(id=1)
返回单个满足条件的对象
2.filter
player = models.Player.objects.filter(id=1)
过滤,返回满足条件的数据
3.count
返回当前查询的总条数
player = models.Player.objects.all().count()
4.first
返回第一个对象
player = models.Player.objects.filter(id=1).first()
5.last
返回最后一个对象
6.exist
判断查询集中是否有数据,如果有则返回True
7.all
返回所有查询出的对象
player = models.Player.objects.all()

  

重点

双下划线起到where的作用
1.exclude(title__contains=‘天‘)
是否包含,等价于 like ‘%天%‘

2.startwith,endwith
以什么开头或结尾
exclude(title__endwith=‘天‘)

3.year,month,day,week_day,hour,minute,second
    filter(pub_data__year=2000)
对日期类型的处理

Q对象
django.db.models import Q
可以使用&(and)与,|(or)或,~(not)非
user.objects.filter(Q(pk=6)|Q(pk=10))

  这些方法解决了,自己需要写sql语句的情况。

普通的增删改查

增:
models.Publisher.objects.create(name = new_name)
删:
del_obj = models.Author.objects.get(id)
del_obj.delete()
改:
edit_author_obj = models.Author.objects.get(id = edit_author_id)
edit_author_obj.name = new_author_name
edit_author_obj.save()

  

django从0开始-02

原文:https://www.cnblogs.com/FlowerNotGiveYou/p/12677203.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!