4

I have a model and I want to write an update() method for it in order to update. The below snippet is my model:

class Klass(models.Model):
    title = models.CharField(max_length=50)
    description = models.CharField(max_length=500)
    university = models.CharField(max_length=50,blank=True, null=True)
    teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE)

and the below snippet is corresponding Serializer:

class KlassSerializer(ModelSerializer):
        teacher = ProfileSerializer()
        url = HyperlinkedIdentityField(view_name='mainp-api:detail', lookup_field='pk')
        klass_settings = KlassSettingsSerializer()

    class Meta:
        model = Klass
        fields = ('url', 'id', 'title', 'description', 'university','teacher')

    
    def update(self, instance, validated_data):
        instance.title = validated_data.get('title', instance.title)
        instance.description = validated_data.get('description', instance.description)
        instance.university = validated_data.get('university', instance.university)
        instance.save()

        return instance

And for update, I use below snippet:

class KlassAPIView(APIView):    
    def put(self, request, pk=None):
        if pk == None:
            return Response({'message': 'You must specify class ID'}, status=HTTP_400_BAD_REQUEST)

        klass = Klass.objects.get(pk=pk)
        if request.user.profile.type != 't':
            raise PermissionDenied(detail={'message': 'You aren't teacher of this class, so you can't edit information.'})

        serializer = KlassSerializer(data=request.data, context={'request': request})
        serializer.initial_data['teacher'] = request.user.profile.__dict__

        if serializer.is_valid():
            serializer.update(instance=klass, validated_data=serializer.data)  # Retrieve teacher and store
            return Response({'data': serializer.data}, status=HTTP_200_OK)
        else:
            return Response({'data': serializer.errors}, status=HTTP_400_BAD_REQUEST)

but when I send data with PUT method, it returns below error:

AttributeError at /api/class/49/

'collections.OrderedDict' object has no attribute 'pk'

and the error occurs in serializer.update(instance=klass, validated_data=serializer.data) line.

Community
  • 1
  • 1
msln
  • 1,318
  • 2
  • 19
  • 38
  • It's not clear what you are doing here. Why are you passing the serializer's data back into itself? – Daniel Roseman Oct 18 '17 at 08:33
  • 1
    you should not explicitly call `update()`. You should just call `save()` – Jahongir Rahmonov Oct 18 '17 at 08:34
  • Also, you don't need to define `update` either; plus, as with forms, initial_data will be ignored if you pass explicit data. This is all sorts of wrong. – Daniel Roseman Oct 18 '17 at 08:38
  • @JahongirRahmonov if I call save(), it creates a new object and a new row in database. I declared `create()` method, and is seems `create()` will be called. – msln Oct 18 '17 at 08:41
  • @msln make sure that you have `id` inside the data that you are `PUT` ting. Then, it will try to update, not create. If it does not have `id`, of course it will try to create one. – Jahongir Rahmonov Oct 18 '17 at 08:43
  • @JahongirRahmonov even though I call `save()` method and send current object `id` or `pk`, it creates a new object and a new row in database. – msln Oct 18 '17 at 08:55
  • i think this happen because in serializer you use `id` instead of `pk` – Mohammad Efazati Oct 18 '17 at 10:42

4 Answers4

16

Just ran into the same error.

In my case the problem was I accessed serializer.data before doing serializer.save().

Google dropped me here, so maybe someone else will also find this helpful.

Source: https://github.com/encode/django-rest-framework/issues/2964

DapperDuck
  • 2,728
  • 1
  • 9
  • 21
MKaras
  • 665
  • 7
  • 17
  • This works for me. In DRF, when basing serializers on models, save() makes sure all changes are saved to database: https://www.django-rest-framework.org/api-guide/serializers/#saving-instances It would seem you cannot access lists in a serializer without saving it to database, which is very weird... anyone knows why? – Alvaro Rodriguez Scelza Jun 18 '20 at 22:24
  • 2
    a workaround without saving to database would be to access serializer.validated_data – Alvaro Rodriguez Scelza Jun 18 '20 at 22:26
2

i don't know if this helps. I always add the id field in the serializer due to that similar issue:

id = serializers.ModelField(model_field=YourModel._meta.get_field('id'), required=False)

Make sure it's required=False because when you create a new record the id field is not present.

Sicco
  • 6,167
  • 5
  • 45
  • 61
G...... T......
  • 139
  • 1
  • 4
2

Well in my case, I was doing:

champions_list = []

for champion in champions_serializer.data:
   c = {"id": champion.id}
   champions_list.append(c)

And the correct way to do it is:

champions_list = []

for champion in champions_serializer.data:
   c = {"id": champion["id"]}
   champions_list.append(c)

And make sure that you return the id inside the serializer.

Jalal
  • 334
  • 1
  • 4
  • 16
Ahmed Safadi
  • 4,402
  • 37
  • 33
1

Many answers to this question note that serializer.save() must be called before using serializer.data.

In my case, I was definitely calling serializer.save(), however, I was overriding the save method on my serializer and did not set self.instance on the serializer in that method.

So if you are overriding save be sure to do:

class MySerializer(serializers.ModelSerializer):
    def save(self, *args, **kwargs):
        ...
        self.instance = instance
        return self.instance
  • Thanks. It helped. Do anyone think that overriding the Serializer.save is a bad practice? – kozhioyrin Feb 16 '23 at 15:13
  • 1
    It's covered in the DRF docs: https://www.django-rest-framework.org/api-guide/serializers/#saving-instances I'm not sure exactly why I was doing the above at the time, but I would have avoided this issue by overriding **create** or **update** since its a ModelSerializer. But in my experience there are times when overriding the save functions makes sense. – riemannnoodles Feb 17 '23 at 17:23