Viewset/Serializer 上的 DRF PUT 不会在模型实例上触发 post_save 信号

DRF PUT on Viewset/Serializer does not fire post_save signal on model instance

我有以下 DRF 视图集:

class MyViewSet(RetrieveModelMixin, ListModelMixin, UpdateModelMixin, GenericViewSet, CreateModelMixin, DestroyModelMixin):
        serializer_class = MySerializer
        queryset = My.objects.all()
        lookup_field = "uuid"

以下序列化程序:

class MySerializer(serializers.ModelSerializer):
    class Meta:
        model = My
        fields = [
            'uuid',
            'name'
        ]

我在应用程序的 signal.py 中存储了一个信号

@receiver(post_save, sender=My)
def my_updated(sender, instance, created, **kwargs):
    if not created:
        print('MySignal Fired')

信号导入apps.py。 当我打开终端并 运行 实例上的 .save() 方法时,信号正在工作。

该应用程序也在 django 配置中声明。 有什么想法吗?

顺便说一句。在 API 上执行 .PUT 时,它会将所有内容很好地保存在数据库中。所以 api 正在做它的工作。不确定为什么信号没有发射。

post_save 不适用于放置和补丁请求..

挂钩通用视图的新函数是:

perform_create(self, serializer) - Called by CreateModelMixin  when saving a new object instance.
perform_update(self, serializer) - Called by UpdateModelMixin when saving an existing object instance.
perform_destroy(self, instance) - Called by DestroyModelMixin when deleting an object instance.

查看文档:Read this

def perform_create(self, serializer):
    serializer.save(user=self.request.user)

def perform_update(self, serializer):
    instance = serializer.save()