在数据库中保存有效的Django表单时如何触发自定义python代码 [英] How to trigger custom python code when a valid django form is saved in the database

查看:36
本文介绍了在数据库中保存有效的Django表单时如何触发自定义python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里Django新手,我按照教程创建了一个简单表单,表格正确地将数据保存在我的Postgres连接的本地数据库中.我想知道,每当将有效表单保存到数据库中时,如何触发函数?我要运行的代码是一个简单的函数,该函数编写在python文件中,并且对第一种形式给出的最新数据进行了一些处理.我希望它仅在保存有效表单数据后才运行,并且想知道django信号触发器是否是我的理想之选.随时要求任何进一步的澄清.换句话说,我想对数据库中存在的,由表单填充的数据进行一些后处理,并且仅当在数据库中输入有效数据时才触发后处理strong>.

Django newbie here, I created a simple form following this tutorial, and my form correctly saves the data in my Postgres connected local database. I was wondering, how can I trigger a function, whenever a valid form is saved into the database? The code I want to run is a simple function which is written in a python file, and it does some processing on the latest data given by the first form. I want it to run only when a valid form data is saved and was wondering if django signal trigger is my way to go. Feel free to ask for any further clarification. In other words, I want to do some post-processing on data, which is present inside the database, which is being filled by the form, and trigger the post-processing only when valid data is entered in the database.

这是我的代码:

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .forms import auditform, ClientAuditForm
from django.db.models.signals import post_save
from . import rocode



# def auditingfun(request):
#     return HttpResponse('Auditing form works')
# # Create your views here.

def auditingfun(request):

    if request.method == 'POST':
        forminput = auditform(request.POST)
        if forminput.is_valid():

            Name = forminput.cleaned_data['Name']
            Origin = forminput.cleaned_data['Origin']
            ClientAddress = forminput.cleaned_data['ClientAddress']
            DispatchType = forminput.cleaned_data['DispatchType']
            ETA = forminput.cleaned_data['ETA']
            GSTIN = forminput.cleaned_data['GSTIN']
            # print(GSTIN,Name,Origin,Destination,MaterialType,Preference,ClientAddress,DispatchType,ETA)

    forminput = auditform(request.POST)
    return render(request, 'auditing/auditform.html', {'forminput': forminput} )

forms.py

from django import forms
from .models import auditModel

class auditform(forms.Form):
    Origin = forms.CharField()
    Destination = forms.CharField()
    MaterialType = forms.CharField()
    Preference = forms.CharField()
    ClientAddress = forms.CharField(widget=forms.Textarea)
    Name = forms.CharField()
    GSTIN = forms.IntegerField()
    DispatchType = forms.ChoiceField(choices=[('Question','Inbound'),('Other','Outbound')])
    ETA = forms.CharField()


class ClientAuditForm(forms.ModelForm):

    class Meta:
            model = auditModel
            fields = ('Origin','Destination','MaterialType','GSTIN','Name','Preference','ClientAddress','DispatchType','ETA')

为简单起见,请想象一下自定义代码(导入到views.py文件中的rocode.py),我刚刚添加了输入的数据,并将数据存储在同一数据库的不同列中.

Just for simplicity, imagine the customcode (imported in the views.py file as rocode.py) I have just adds the data entered and stores the data in the same database, in a different column.

推荐答案

此处要使用的是信号.信号是在数据库中添加或更新项目后执行的某些功能.假设您要连接的模型称为"MyModel",请执行以下操作:

what you want to use here is signals. A signal is some function that gets executed after an item is added or updated in your data base. Assuming your model you want to connect to is called "MyModel" do this:

from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import MyModel


@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, **kwargs):
    if created:
        # run your custom code HERE

实例是插入/更新的对象,创建的是布尔值,指示这是更新还是插入.

instance is what was inserted / updated, created is boolean indicating if this was an update or insert.

docs: https://docs.djangoproject.com/en/2.1/主题/信号/

这篇关于在数据库中保存有效的Django表单时如何触发自定义python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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