您现在的位置是: 网站首页 >Django >Vue+Django REST framework前后端分离生鲜电商 Django

【Vue+DRF生鲜电商】05.商品列表序列化普通方法

admin2019年4月21日 21:41 Django | Python | Vue 1596人已围观

Vue+Django REST framework前后端分离生鲜电商简介 Vue+Django REST framework 打造前后端分离的生鲜电商项目(慕课网视频)。 Github地址:https://github.com/xyliurui/DjangoOnlineFreshSupermarket ; Django版本:2.2、djangorestframework:3.9.2。 前端Vue模板可以直接联系我拿。

## 普通视图序列化 ### JSONView 访问 https://github.com/gildas-lormeau/JSONView-for-Chrome 下载 选择插件目录 JSONView-for-Chrome-master\WebContent ![BLOG_20190421_214417_38](/media/blog/images/2019/04/BLOG_20190421_214417_38.png "博客图集BLOG_20190421_214417_38.png") ### View实现商品列表-json 在goods应用下创建 views_base.py 文件,增加一下内容 ```python from django.views.generic.base import View from django.views.generic import ListView from goods.models import Goods class GoodsListView(View): def get(self, request): """ 通过Django的View获取商品列表页 :param request: :return: """ json_list = list() all_goods = Goods.objects.all()[:5] print(all_goods) for goods in all_goods: json_dict = dict() json_dict['name'] = goods.name json_dict['category'] = goods.category.name json_dict['shop_price'] = goods.shop_price json_list.append(json_dict) from django.http import HttpResponse import json return HttpResponse(json.dumps(json_list), content_type='application/json') ``` 修改项目配置文件夹的主 urls.py ,增加该视图的url ```python from goods.views_base import GoodsListView urlpatterns = [ path('admin/', admin.site.urls), path('api-auth/', include('rest_framework.urls')), path('ckeditor/', include('ckeditor_uploader.urls')), # 配置富文本编辑器url path('goods/', GoodsListView.as_view(), name='goods_list') ] ``` 访问 http://127.0.0.1:8000/goods/ ![BLOG_20190421_214359_24](/media/blog/images/2019/04/BLOG_20190421_214359_24.png "博客图集BLOG_20190421_214359_24.png") 但这种方式需要将每个字段加到json中去,如果遇到`DateTimeField`字段,是不能序列化的。 ### View实现商品列表-model_to_dict 解决字段多的情况,在我们的模型中,如果字段多,像上面的方法,是非常麻烦的。 ```python from django.views.generic.base import View from django.views.generic import ListView from goods.models import Goods class GoodsListView(View): def get(self, request): """ 通过Django的View获取商品列表页 :param request: :return: """ json_list = list() all_goods = Goods.objects.all()[:5] # print(all_goods) # for goods in all_goods: # json_dict = dict() # json_dict['name'] = goods.name # json_dict['category'] = goods.category.name # json_dict['shop_price'] = goods.shop_price # json_list.append(json_dict) from django.forms.models import model_to_dict for goods in all_goods: json_dict = model_to_dict(instance=goods) json_list.append(json_dict) from django.http import HttpResponse import json return HttpResponse(json.dumps(json_list), content_type='application/json') ``` 以上方式虽然能获取所有字段,但对于`ImageField`和`DateTimeField`仍然是不支持的 ### 实现商品列表-serializers ```python class GoodsListView(View): def get(self, request): """ 通过Django的View获取商品列表页 :param request: :return: """ json_list = list() all_goods = Goods.objects.all()[:5] # print(all_goods) # for goods in all_goods: # json_dict = dict() # json_dict['name'] = goods.name # json_dict['category'] = goods.category.name # json_dict['shop_price'] = goods.shop_price # json_list.append(json_dict) # from django.forms.models import model_to_dict # for goods in all_goods: # json_dict = model_to_dict(instance=goods) # json_list.append(json_dict) from django.core import serializers json_data = serializers.serialize('json', all_goods) from django.http import HttpResponse, JsonResponse import json return HttpResponse(json_data, content_type='application/json') ``` ![BLOG_20190421_214339_30](/media/blog/images/2019/04/BLOG_20190421_214339_30.png "博客图集BLOG_20190421_214339_30.png") 再使用`JsonResponse` ```python class GoodsListView(View): def get(self, request): """ 通过Django的View获取商品列表页 :param request: :return: """ json_list = list() all_goods = Goods.objects.all()[:5] # print(all_goods) # for goods in all_goods: # json_dict = dict() # json_dict['name'] = goods.name # json_dict['category'] = goods.category.name # json_dict['shop_price'] = goods.shop_price # json_list.append(json_dict) # from django.forms.models import model_to_dict # for goods in all_goods: # json_dict = model_to_dict(instance=goods) # json_list.append(json_dict) from django.core import serializers json_data = serializers.serialize('json', all_goods) # 序列化 from django.http import HttpResponse, JsonResponse import json # return HttpResponse(json_data, content_type='application/json') json_data = json.loads(json_data) # 转换为数组 return JsonResponse(json_data) ``` 会出现错误:`TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False.` 可以进行如下配置 ```python return JsonResponse(json_data) # 修改为 return JsonResponse(json_data, safe=False) ``` 同样也可以正常显示json数据 ### 使用serializer缺点 ![BLOG_20190421_214316_38](/media/blog/images/2019/04/BLOG_20190421_214316_38.png "博客图集BLOG_20190421_214316_38.png") - 比如图片,这保存的是一个相对路径,必须手动在前面加上 settings.py 中配置的`media` - 字段,序列化顺序固定 - 没有文档功能

很赞哦! (1)

文章交流

  • emoji
0人参与,0条评论

当前用户

未登录,点击   登录

站点信息

  • 建站时间:网站已运行2247天
  • 系统信息:Linux
  • 后台程序:Python: 3.8.10
  • 网站框架:Django: 3.2.6
  • 文章统计:257 篇
  • 文章评论:60 条
  • 腾讯分析网站概况-腾讯分析
  • 百度统计网站概况-百度统计
  • 公众号:微信扫描二维码,关注我们
  • QQ群:QQ加群,下载网站的学习源码
返回
顶部
标题 换行 登录
网站