简体   繁体   中英

Django Rest Framework, updating multiple objects in one

I am trying to update multiple objects using PATCH to my Django backend. This is the request I am sending:

[
   {
      "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
   },
]

I send this to a custom view where I am trying to serialize and update the data.

@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)

I only end up getting this error:

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

It seems like this error comes up when you are looking for one object, but I have many=True . What am I doing wrong?

ListSerializer can solve the problem. Here's how:

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

Then you need to specify the ListSerializer in the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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