繁体   English   中英

Django Rest 框架,多对象合一更新

[英]Django Rest Framework, updating multiple objects in one

我正在尝试使用PATCH将多个对象更新到我的 Django 后端。 这是我发送的请求:

[
   {
      "pk":78,
      "weekday":1,
      "from_hour":"21:00",
      "to_hour":"12:00:00",
      "closed":false,
      "lunch":true,
      "lunch_start":null,
      "lunch_end":null,
      "lunch2":false,
      "lunch_start2":null,
      "lunch_end2":null,
      "appointment_interval":15,
      "num_appointments_interval":4,
      "office":79
   },
   {
      "pk":79,
      "weekday":2,
      "from_hour":"09:00:00",
      "to_hour":"12:00:00",
      "closed":false,
      "lunch":true,
      "lunch_start":null,
      "lunch_end":null,
      "lunch2":false,
      "lunch_start2":null,
      "lunch_end2":null,
      "appointment_interval":15,
      "num_appointments_interval":4,
      "office":79
   },
   {
      "pk":80,
      "weekday":3,
      "from_hour":"09:00:00",
      "to_hour":"12:00:00",
      "closed":false,
      "lunch":true,
      "lunch_start":null,
      "lunch_end":null,
      "lunch2":false,
      "lunch_start2":null,
      "lunch_end2":null,
      "appointment_interval":15,
      "num_appointments_interval":4,
      "office":79
   },
   {
      "pk":81,
      "weekday":4,
      "from_hour":"09:00:00",
      "to_hour":"12:00:00",
      "closed":false,
      "lunch":false,
      "lunch_start":"14:59:50",
      "lunch_end":"14:59:51",
      "lunch2":false,
      "lunch_start2":null,
      "lunch_end2":null,
      "appointment_interval":15,
      "num_appointments_interval":4,
      "office":79
   },
]

我将其发送到我试图序列化和更新数据的自定义视图。

@api_view(['PATCH'])
@parser_classes((JSONParser,))
def updateOfficeHours(request):
    office_id = request.data[0]['office']
    qs = OfficeHour.objects.filter(office__pk=office_id)
    office_hours = OfficeHoursSerializer(qs, data=request.data, many=True, partial=True)
    
    if not office_hours.is_valid():
        print(":(")
        return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    else:
    

        office_hours.save()
    return Response(status=status.HTTP_200_OK)

我最终只得到这个错误:

AttributeError: 'QuerySet' object has no attribute 'pk'

当您正在寻找一个 object 时,似乎会出现此错误,但我有many=True 我究竟做错了什么?

ListSerializer可以解决这个问题。 就是这样:

class OfficeHoursListSerializer(serializers.ListSerializer):
    def update(self, instances, validated_data):
         # here you can implement your own logic for updating objects
         # this is just an example
         result = []

         for instance in instances:
             for data in validated_data:
                 if data['id'] == instance.pk:
                     instance.some_field = data['some_field']
                     instance.save()
                     result.append(instance)

         return result

然后您需要在 OfficeHoursSerializer 中指定OfficeHoursSerializer

class OfficeHoursSerializer(serializers.ModelSerializer):
    # It needs to identify elements in the list using their primary key,
    # so use a writable field here, rather than the default which would be read-only.
    id = serializers.IntegerField()
    ...

    class Meta:
        ...
        list_serializer_class = OfficeHoursListSerializer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM