简体   繁体   English

将一些代码从 GenericAPIView post 方法移动到 django 中的其他方法

[英]Move some code from GenericAPIView post method to other methods in django

I have my django GenericAPIView with post method, I want some part of the logic which uses self to be in some other method and exccute that code when my post method gets hit我有我的 django GenericAPIView 和 post 方法,我想要一些使用 self 的逻辑在其他方法中,并在我的 post 方法被命中时执行该代码

class MyAPIView(GenericAPIView):
    def post(self, request, *args, **kwargs):
        

You can do something like this!你可以做这样的事情!

class MyAPIView(CreateAPIView): class MyAPIView(创建APIView):

def custom_function(self):
    """place your logic here!"""
    pass

def post(self, request, *args, **kwargs):
    # This will call our custom function as soon as post request is hit
    self.custom_function()
    return super().post(request, *kwargs, *args)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一些代码从 post 方法移动到 Django 视图中的单独方法 - How to move some code from post method to a separate method in Django views 如何在 django rest 框架中的 post 方法中添加一些自定义代码 - How to add some cutom code to post method in django rest framework 使 Django 中的其他方法(和视图)可以访问一种方法的上下文变量 - making context variable from one method accessible to other other methods (and views) in Django 从Django应用程序中的其他方法访问变量 - Accessing variables from other methods in a django app 防止在 django class 花瓶视图中获取和发布方法中的重复代码 - Prevent repetition code in methods get and post in django class vase view Django - 从其他URL获取POST数据 - Django - Get POST data from other URL Python - 将文件从一个文件夹移动到另一个文件夹,但有一些例外 - Python - Move files from one folder to other with some exceptions anaconda.conda 文件夹从 /home/usrxy 移动到其他位置 - anaconda .conda folder move from /home/usrxy to some other location 将POST方法从Flask更改为Django - Change POST method from Flask to Django Django / Python,使用contenttypes或其他方法,使数据库保存功能可重复使用(以便从字符串中获取模型名和应用名)? - Django / Python, Make database save function re-usable (so that it takes modelname and appname from strings), using contenttypes or some other method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM