12 实战 vue drf

model2json

  1. 自定义字典
  2. from django.forms.models import model_to_dict
  3. from django.core import serializers
  1. json_data = serializers.serialize("json", "goods")
  1. JSONResponse

vue

drf

依赖

  1. coreapi (1.32.0+) - Schema generation support.
  2. Markdown (2.1.0+) - Markdown support for the browsable API.
  3. django-filter (1.0.1+) - Filtering support.
  4. django-crispy-forms - Improved HTML display for filtering.
  5. django-guardian (1.1.1+) - Object level permissions support.
  6. pip install djangorestframework
  7. pip install markdown # Markdown support for the browsable API.
  8. pip install django-filter # Filtering support

ModelSerializers

  1. # 序列化全部/部分字段
  2. class GoodsSerializer(serializers.ModelSerializers):
  3. class Meta:
  4. model = Goods
  5. # fields = "__all__"
  6. fields = ('name', 'click_num', 'market_price', 'add_time')
  7. # 序列化嵌套
  8. class CategorySerializer(serializers.ModelSerializers):
  9. class Meta:
  10. model = GoodsCategory
  11. fields = "__all__"
  12. class GoodsSerializer(serializers.ModelSerializers):
  13. Category = CategorySerializer()
  14. class Meta:
  15. model = Goods
  16. fields = "__all__"