数据增删改查操作语句:
# 条件查询
"""
# 增加数据(详见数据导入笔记,里面有多种方法)
CourseModel.objects.create(cour_id=11, course='java', grade='95')
# 查询所有数据:
subject = CourseModel.objects.all()
# filter() 方法用于查询符合条件的数据
subject = CourseModel.objects.filter(cour_id='20201010')
# 获取单个对象(数据)
sub = CourseModel.objects.get(id=330)
# 数据排序
subject = CourseModel.objects.order_by("-id") 正序排序
subject = CourseModel.objects.order_by("-id") 倒序排序
# 修改数据
sub = CourseModel.objects.get(id='345')
sub.course = "python"
sub.save()
# 删除数据
sub = CourseModel.objects.get(id='345')
sub.delete()
"""
具体详细代码示例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:ZSW
@file:query2db.py
@time:2020/10/10
增删改查操作
"""
import simplejson as json
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_demo.settings")
'''
Django 版本大于等于1.7的时候,需要加上下面两句
import django
django.setup()
否则会抛出错误 django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
'''
import django
if django.VERSION >= (1, 7): # 自动判断版本
django.setup()
### 增加数据 单条导入方法1(推荐)
def add_data():
from django_demo.models import CourseModel
CourseModel.objects.create(cour_id=11, course='java', grade='95')
### 查询数据
# 返回所有数据
def query_data():
from django_demo.models import CourseModel
subject = CourseModel.objects.all()
list1 = []
for sub in subject:
# print(sub.cour_id, sub.course, sub.grade)
data = {"id":sub.id, "课程id":sub.cour_id, "课程":sub.course, "成绩":sub.grade}
list1.append(data)
res = {'code': 1, 'message': '成功', 'data': list1}
return json.dumps(res, ensure_ascii=False, indent=4)
def query_data1():
from django_demo.models import CourseModel
list1 = []
# filter() 方法用于查询符合条件的数据
subject = CourseModel.objects.filter(cour_id='20201010')
print(subject)
for sub in subject:
data = {"id": sub.id, "课程id": sub.cour_id, "课程": sub.course, "成绩": sub.grade}
list1.append(data)
res = {'code': 1, 'message': '成功', 'data': list1}
return json.dumps(res, ensure_ascii=False, indent=4)
# 查询单条数据
def query_data2():
from django_demo.models import CourseModel
list1 = []
# filter() 方法用于查询符合条件的数据
sub = CourseModel.objects.get(id=330)
data = {"id": sub.id, "课程id": sub.cour_id, "课程": sub.course, "成绩": sub.grade}
res = {'code': 1, 'message': '成功', 'data': data}
return json.dumps(res, ensure_ascii=False, indent=4)
# 数据排序
def query_data3():
from django_demo.models import CourseModel
list1 = []
# filter() 方法用于查询符合条件的数据
subject = CourseModel.objects.order_by("-id")
for sub in subject:
data = {"id": sub.id, "课程id": sub.cour_id, "课程": sub.course, "成绩": sub.grade}
list1.append(data)
res = {'code': 1, 'message': '成功', 'data': list1}
return json.dumps(res, ensure_ascii=False, indent=4)
### 修改数据(先查找后修改)
def update_data():
from django_demo.models import CourseModel
sub = CourseModel.objects.get(id='345')
sub.course = "python"
sub.save()
### 删除数据:
def delete_data():
from django_demo.models import CourseModel
sub = CourseModel.objects.get(id='345')
sub.delete()
if __name__ == '__main__':
# add_data()
# res = query_data()
# res = query_data1()
# res = query_data2()
# res = query_data3()
# print(res)
# update_data()
delete_data()
print("Done!")
query_data1() 返回结果示例:
<QuerySet [<CourseModel: 学生Id: 课程: 分数: >, <CourseModel: 学生Id: 课程: 分数: >, <CourseModel: 学生Id: 课程: 分数: >, <CourseModel: 学生Id: 课程: 分数: >]>
{
"code": 1,
"message": "成功",
"data": [
{
"id": 329,
"课程id": "20201010",
"课程": "英语",
"成绩": 132
},
{
"id": 333,
"课程id": "20201010",
"课程": "英语",
"成绩": 120
},
{
"id": 337,
"课程id": "20201010",
"课程": "英语",
"成绩": 104
},
{
"id": 341,
"课程id": "20201010",
"课程": "英语",
"成绩": 100
}
]
}