2018-04-02

2018-04-02  本文已影响1人  你常不走的路

PostSerializer 给list用 序列化外键
PostSerializer2 给update用 更新是 外建同样可以选择 更改 如果不加Category.objects.all(),many=True 只会显示自己外建不能显示所有外建进行选择
PostSerializer3 给create用

class CategorySerializer2(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'
        read_only_fields = ['created_at']

class PostSerializer(serializers.ModelSerializer):
    category = CategorySerializer2()
    class Meta:
        model = Posts
        fields = '__all__'

class PostSerializer2(serializers.ModelSerializer):
    category = CategorySerializer2(Category.objects.all(),many=True ).data
    class Meta:
        model = Posts
        fields = '__all__'
        read_only_fields = ['created_at']

class PostSerializer3(serializers.ModelSerializer):
    class Meta:
        model = Posts
        fields = '__all__'
        read_only_fields = ['created_at']

上一篇下一篇

猜你喜欢

热点阅读