社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

Django DRF使用CreateListModelMixin更改序列化程序数据

Martin • 5 年前 • 1437 次点击  

我有这个类视图,但无法修改序列化程序数据以插入更多数据(这是必需的,需要自动填充)。 因为我一次创建了许多实例,所以序列化程序基于kwargs['many']=true。

知道如何向每个序列化程序数据添加另一个字段吗?

谢谢,

:

class ReservedServiceView(CreateListModelMixin, ModelViewSet):
queryset = ReservedService.objects.all()
serializer_class = ReservedServiceSerializer
authentication_classes = (authentication.TokenAuthentication,)

def perform_create(self, serializer):
    # Create an event that is a Reflection of the Reserved Service
    serializer_data = self.request.data

    for reserved_service in serializer_data:
        print("--------",reserved_service, flush=True)

        service_id = reserved_service['original_service']

        original_service = Service.objects.get(pk=service_id)

        calendar_event = CalendarEvent()
        calendar_event.name = original_service.name
        calendar_event.description = original_service.description
        calendar_event.date = reserved_service['date']
        calendar_event.type_id = 1
        calendar_event.start_time = reserved_service['start_time']
        calendar_event.end_time = reserved_service['end_time']
        calendar_event.location_id = original_service.location.id
        calendar_event.save()

        reserved_service['associated_event'] = calendar_event.id

    print("**********1", serializer_data)
    print("**********2", self.request.data)
    serializer.save()

基于:

class CreateListModelMixin(object):
def get_serializer(self, *args, **kwargs):
    """ if an array is passed, set serializer to many """
    if isinstance(kwargs.get('data', {}), list):
        kwargs['many'] = True
    return super(CreateListModelMixin, self).get_serializer(*args, **kwargs)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40736
 
1437 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Atul Mishra
Reply   •   1 楼
Atul Mishra    6 年前

我无法正确地获取您的问题,但是如果您的问题是您没有获取在视图的响应中添加到序列化程序的额外字段,那么这里是它的答案。

此视图的响应由的create方法返回 CreateModelMixin 哪些通过 serializer.data data 帕拉姆 Response . 你不能更新 序列化程序.data 因为它是一个不可变的对象。所以,要解决这个问题,你必须重写 create 方法如下:

class ReservedServiceView(CreateListModelMixin, ModelViewSet):
    queryset = ReservedService.objects.all()
    serializer_class = ReservedServiceSerializer
    authentication_classes = (authentication.TokenAuthentication,)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        my_data = {}.update(serializer.validated_data)
        # Now you can work over the my_data and add extra fields to it and save it 
        # and instead of passing serializer.data we pass my_data to Response class
        headers = self.get_success_headers(serializer.data)
        return Response(my_data, status=status.HTTP_201_CREATED, headers=headers)