day42-Django

2018-09-11  本文已影响0人  七一欧

day2_init_.py

import pymysql

pymysql.install_as_MySQLdb()
这是pymysql的驱动

数据的增删改查
1、数据的写入

Student.objects.create(s_name='大红')
stu = Student()
stu.s_name = '小龙'
stu.save()
stu = Student('小花', 18, 1)
stu.save()

2、数据的查找

stus = Student.objects.all()

filter:获取的结果为queryset。可以返回空或者一条或多条数据

stus = Student.objects.filter(s_name='小花')
   stus = Student.objects.filter(s_age=19).filter(s_name='小花')
    stus = Student.objects.filter(s_age=19,s_name='小花')
 stus = Student.objects.exclude(s_name='小花')
  stus = Student.objects.all.order_by('id')
 print(stus)
 stus = Student.objects.all.order_by('-id')
 stus = Student.Objects.all().values('id', 's_name', 's_age')

模糊搜索\

  stus = Student.objects.filter(s_name__contains='花')
 stus = Student.objects.filter(s_name__startswith='花')
 stus = Student.objects.filter(s_name__endswith='花')
stus = Student.objects.filter(id_in=[1,2,3])
stus= Student.objects.filter(s_age__gt=18)
stus = Student.objects.filter(id=1)
stus = Student.objects.filter(pk=1)
stus = Student.objects.filter(Q(s_name='小花')| Q(s_age=18))

3、删除

stu = Student.objects.get(pk=5)
stu.delete()
Student.objects.filter(id=2).first.delete()

4、修改

    stu = Student.objects.get(pk=1)
    stu.s_name = '帅哥'
    stu.save()
    Student.objects.filter(id=1).update(s_name='嘻嘻')
    return HttpResponse('修改')
上一篇 下一篇

猜你喜欢

热点阅读