Files

15 lines
592 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""核心应用通用组件:async 序列化器基类(adrf 0.1.14+ 需要 asave/acreate)。"""
from rest_framework import serializers as drf_serializers
from asgiref.sync import sync_to_async
class AsyncModelSerializer(drf_serializers.ModelSerializer):
"""给所有需要的 model serializer 加 asave / acreate(adrf async view 调用的接口)。"""
async def asave(self, **kwargs):
return await sync_to_async(self.save)(**kwargs)
async def acreate(self, validated_data):
return await sync_to_async(self.Meta.model.objects.create)(**validated_data)