导入:from django.views import View

一、查询所有数据

  • 查询数据在自定义的视图类中定义get方法
  • 使用django.http模块中的JsonResponse对非json格式的数据做返回处理
  • 在JsonResponse必须添加safe=False参数,否则会报错:In order to allow non-dict objects to be serialized set the safe parameter to False. ```python from django.http import HttpResponse from django import http

Create your views here.

class UserView(View): ‘’’ 用户视图 ‘’’

  1. def get(self, request):
  2. # 模型类实例化对象
  3. users = UserProfile.objects.all()
  4. user_list = []
  5. for user in users:
  6. user_dict = {
  7. 'id': user.id,
  8. 'username': user.username,
  9. 'password': user.password,
  10. 'open_id': user.open_id,
  11. 'code': user.code
  12. }
  13. user_list.append(user_dict)
  14. return http.JsonResponse(user_list)
  1. <a name="jtj9b"></a>
  2. ### 二、创建数据
  3. - 使用django中的json,把前端传递过来的json数据转成字典
  4. - 使用django.db.models模块中的Q来查询多个字段在数据库中是否存在
  5. ```python
  6. from django.views import View
  7. from django.http import HttpResponse
  8. from django import http
  9. from django.db.models import Q
  10. import json
  11. class UserView(View):
  12. ''' 用户视图 '''
  13. def post(self, request):
  14. # 获取数据, json转字典
  15. dict_data = json.loads(request.body.decode())
  16. print(dict_data)
  17. nick_name = dict_data.get('nickName')
  18. code = dict_data.get('code')
  19. open_id = "xljsafwjeilnvaiwogjirgnlg"
  20. # 校验数据
  21. result = UserProfile.objects.filter(Q(code=code) | Q(open_id=open_id))
  22. if not result.exists():
  23. # 数据入库
  24. user = UserProfile.objects.create(
  25. username=nick_name,
  26. open_id=open_id,
  27. code=code
  28. )
  29. # 返回响应
  30. user_dict = {
  31. 'id': user.id,
  32. 'username': user.username,
  33. 'password': user.password,
  34. 'open_id': user.open_id,
  35. 'code': user.code
  36. }
  37. return http.JsonResponse(user_dict)
  38. return http.JsonResponse("用户已存在", safe=False, status=202)

三、查询某一条数据(单个)

  • 前端需要传递pk/id值,通过pk/id查询数据,查询一条数据必须用get,不能用filter,否则会报错:AttributeError: ‘QuerySet’ object has no attribute ‘id’
  • 数据转换
  • 返回响应 ```python class UserProfileDetail(View): ‘’’ 详情视图 ‘’’

    def get(self, request):

    1. userInfo = UserProfile.objects.get(id=id)
    2. if not userInfo:
    3. return HttpResponse("查询的用Info户不存在", status=404)
    4. user_dict = {
    5. 'id': userInfo.id,
    6. 'username': userInfo.username,
    7. 'password': userInfo.password,
    8. 'open_id': userInfo.open_id,
    9. 'code': userInfo.code
    10. }
    11. return http.JsonResponse(user_dict, status=200)
  1. <a name="y1FsC"></a>
  2. ### 四、更新一条数据
  3. - 前端需要传递pk/id值,通过pk/id查询数据,查询一条数据必须用get,不能用filter,否则会报错:AttributeError: 'QuerySet' object has no attribute 'id'
  4. - 更新一条数据时必须使用filter来查询数据集,再使用update(**data)来更新数据,不能使用get,否则会报错:AttributeError: '模型类' object has no attribute 'update'
  5. > get查询获取到的是数据对象,而filter查询获取到的是数据集
  6. ```python
  7. class UserProfileDetail(View):
  8. ''' 详情视图 '''
  9. def put(self, request, id):
  10. data_dict = json.loads(request.body.decode())
  11. userInfo = UserProfile.objects.get(id=id)
  12. if not userInfo:
  13. return HttpResponse("查询的用Info户不存在", status=404)
  14. UserProfile.objects.filter(id=id).update(**data_dict)
  15. userInfo = UserProfile.objects.get(id=id)
  16. user_dict = {
  17. 'id': userInfo.id,
  18. 'username': userInfo.username,
  19. 'password': userInfo.password,
  20. 'open_id': userInfo.open_id,
  21. 'code': userInfo.code
  22. }
  23. return http.JsonResponse(user_dict, status=200)

五、删除某一条数据

  1. class UserProfileDetail(View):
  2. ''' 详情视图 '''
  3. def delete(self, request, id):
  4. userInfo = UserProfile.objects.filter(id=id)
  5. if not userInfo:
  6. return HttpResponse("删除的数据不存在", status=404)
  7. UserProfile.objects.filter(id=id).delete()
  8. return HttpResponse("数据删除成功", status=204)

上述的操作只能适用于数据表中字段很少的情况,如果字段较多,写起来会很麻烦,不利于开发