增加记录的两种方式:

    1. Vocation.objects.create(name="奥里给", age=29)
    2. new_vocation = Vocation(name="奥里给", age=29)
    3. new_vocation.save()

    删除记录:

    1. Vocation.objects.filter(id=1).delete()

    修改记录:

    1. Vocation.objects.filter(id=1).update(name="zhangsan")
    2. new_voc = Vocation.objects.get(id=1)
    3. new_voc.name = "laowang"
    4. new_voc.age = 26
    5. new_voc.save()

    获取记录:

    1. Vocation.objects.filter.all()
    2. Vocation.objects.filter(id=1)
    3. Vocation.objects.get(id=1)
    4. Vocation.objects.filter(id__in=[1, 2, 3])
    5. Vocation.objects.exclude(id__in=[2, 3])
    6. # 查询1<id<5的记录
    7. Vocation.objects.filter(id__gt=1, id__lt=5)
    8. Vocation.objects.order_by("name") #按name正序
    9. Vocation.objects.order_by("-name") #按name倒序
    10. Vocation.objects.distinct() #去重

    objects.all()、objects.values()、objects.values_list()区别:

    1. d1 = Department.objects.all()
    2. d2 = Department.objects.values()
    3. d3 = Department.objects.values_list()
    4. d4 = Department.objects.values_list("title")
    5. print(d1) --><QuerySet [<Department: Department object (2)>, <Department: Department object (1)>]> #(all())
    6. print(d2) --><QuerySet [{'id': 2, 'title': 'C++'}, {'id': 1, 'title': 'java'}]> #(values())
    7. print(d3) --><QuerySet [(2, 'C++'), (1, 'java')]> #(values_list())
    8. print(d4)--><QuerySet [('C++',), ('java',)]>

    all()返回对象序列
    values()返回字典类型序列
    values_list()返回元组类型序列

    返回值为QuerySet的函数不会立即去数据库操作数据,只有当需要使用QuerySet里面的值时才回去数据库中获取数据。

    查询单条记录:

    1. Vocation.objects.get(id=1)
    2. Vocation.objects.filter(id=1).first()
    3. Vocation.objects.last()
    4. Vocation.objects.count()