在Django管理网站中调用方法 [英] call a method in Django admin site

查看:79
本文介绍了在Django管理网站中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django项目,现在一切正常。我有一个Django管理站点,现在,我希望当我向模型中添加新记录时,一个函数同时调用并启动一个过程。我该怎么做?这个动作名称是什么?

i have a Django project and right now everything works fine. i have a Django admin site and now, i want that when i add a new record to my model, a function calls simultaneously and a process starts. how i can do this? what is this actions name?

推荐答案


  • 1种方式

    • 1 WAY
    • 您可以通过使用django信号进入您的models.py进入您的应用程序。

      You can go to your models.py into your app by using django signal you can do this.

      
      from django.db.models.signals import post_save
      
      class Test(models.Model):
          # ... fields here
      
      # method for updating
      def update_on_test(sender, instance, **kwargs):
           # custome operation as you want to perform
      
      # register the signal
      post_save.connect(update_on_test, sender=Test)
      




      • 2种方式

        • 2 WAY
        • 如果要填充数据,可以使用modeladmin类的save()方法

          You can ovveride save() method of modeladmin class if you are filling data into table by using django admin.

          
          class TestAdmin( admin.ModelAdmin ):
              fields  = ['title', 'body' ]
              form = TestForm
          
              def save_model(self, request, obj, form, change):
                   # your login if you want to perform some comutation on save 
                   # it will help you if you need request into your work
                   obj.save()
          

          这篇关于在Django管理网站中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆