12 实战 vue drf
model2json
- 自定义字典
- from django.forms.models import model_to_dict
- from django.core import serializers
json_data = serializers.serialize("json", "goods")
- JSONResponse
vue
drf
依赖
coreapi (1.32.0+) - Schema generation support.
Markdown (2.1.0+) - Markdown support for the browsable API.
django-filter (1.0.1+) - Filtering support.
django-crispy-forms - Improved HTML display for filtering.
django-guardian (1.1.1+) - Object level permissions support.
pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support
ModelSerializers
# 序列化全部/部分字段
class GoodsSerializer(serializers.ModelSerializers):
class Meta:
model = Goods
# fields = "__all__"
fields = ('name', 'click_num', 'market_price', 'add_time')
# 序列化嵌套
class CategorySerializer(serializers.ModelSerializers):
class Meta:
model = GoodsCategory
fields = "__all__"
class GoodsSerializer(serializers.ModelSerializers):
Category = CategorySerializer()
class Meta:
model = Goods
fields = "__all__"