使用:
class PostSerializer(serializers.ModelSerializer):
author = ExtUserSerializer(required=False, allow_null=True)
class Meta:
model = Post
field = ('title','description','rating','author')
def create(self, validated_data):
return Post.objects.create(**validated_data)
和
@api_view(['GET'])
def post_list(request, format=None):
if request.method == 'GET':
posts = Post.objects.all()
serializer = PostSerializer(posts, many=True)
return Response(serializer.data)
如何修改Serializer并查看以自动填写字段作者