python - 在 Python 发出 post 请求时如何调用函数?
问题描述
我使用 django rest 框架进行编程。由于 DRF 是 CRUD 自动的,因此我创建了一个如下所示的视图。
class PostViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
serializer_class = PostSerializer
queryset = AllfindPost.objects.all()
顺便说一句,我想在发出 post 请求时调用以下函数。
def send_fcm_notification(ids, title, body):
url = 'https://fcm.googleapis.com/fcm/send'
headers = {
'Authorization': 'key=',
'Content-Type': 'application/json; UTF-8',
}
content = {
'registration_ids': '',
'notification': {
'title': 'title',
'body': 'body'
}
}
requests.post(url, data=json.dumps(content), headers=headers)
我应该怎么办?
解决方案
尝试这个。发布请求后调用您的函数。
def call_my_function():
pass
class PostViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
serializer_class = PostSerializer
queryset = AllfindPost.objects.all()
"""
Create a model instance.
"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
#call your function Eg.
call_my_function()
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
推荐阅读
- r - ggplot:如何使 aes 中的颜色将参数作为颜色的十六进制代码而不是将其用作一个因素?
- javascript - 从网页中恢复显示的 JSON 数据?
- python - Audit Script function.sample 查询?
- python - AttributeError:“列表”对象没有属性“拆分”。Python,通过换行将列表拆分为 .csv 单元格
- html - 在 Bootstrap 4 中,带有浮动的 3 列布局不会更改为带有媒体查询的一列
- asp.net-mvc - 如何将对象值转换为double?
- javascript - 在按下搜索按钮-SAP UI5 时将 Odata 实体集显示为表格
- python - 如何从 txt 读取字节文字?
- python - gcloud app deploy:这个部署有太多的文件 - 如何知道这些 10k+ 文件是什么?
- python - 如何拆分由多个线串组成的shapefile