API response is showing outdated data with PUT/PATCH requests

Viewed 280

I am working on an API using Django REST framework. In my case, I am using nested serializers so I am required to overwrite the .update method.

Here's the required desc:

Serializers

Config Serializer

class DeviceConfigSerializer(serializers.ModelSerializer):
    config = serializers.JSONField()
    context = serializers.JSONField()

    class Meta:
        model = Config
        fields = ['backend', 'status', 'templates', 'context', 'config']

Device Detail Serializer

class DeviceDetailSerializer(serializers.ModelSerializer):
    config = DeviceConfigSerializer()

    class Meta(BaseMeta):
        model = Device
        fields = [
            'id',
            'name',
            'organization',
            'mac_address',
            'key',
            'last_ip',
            'management_ip',
            'model',
            'os',
            'system',
            'notes',
            'config',
        ]

    def update(self, instance, validated_data):
        config_data = None

        if self.data['config'] is None and validated_data.get('config'):
            config_data_ = dict(validated_data.get('config'))
            config_templates = config_data_.pop('templates')
            config = Config.objects.create(device=instance, **config_data_)
            for template in config_templates:
                config.templates.add(template.pk)

        if validated_data.get('config'):
            config_data = validated_data.pop('config')

        # When config data is provided with PATCH requests
        if config_data:
            instance.config.backend = config_data.get(
                'backend', instance.config.backend
            )
            instance.config.status = config_data.get('status', instance.config.status)

            config_templates = config_data.get('templates')
            instance.config.templates.clear()
            for template in config_templates:
                instance.config.templates.add(template.pk)

            instance.config.context = json.loads(
                json.dumps(config_data.get('context')),
                object_pairs_hook=collections.OrderedDict,
            )
            instance.config.config = json.loads(
                json.dumps(config_data.get('config')),
                object_pairs_hook=collections.OrderedDict,
            )
            instance.config.save()

        return super().update(instance, validated_data)

Device Detail Views

class DeviceDetailView(RetrieveUpdateDestroyAPIView):
    serializer_class = DeviceDetailSerializer
    queryset = Device.objects.all()

Now when I Send a PUT/PATCH request to its endpoint, It works fine and the fields in the database get updated, but since after the PUT/PATCH request gets completed successfully I should be returned with the updated instance fields in the response, But in my case, I am required to again send a GET request/ ore refresh the page to get the updated data in the Browsable API. Is there anything more I need to add, or am I missing something?

0 Answers
Related