How to trigger custom python code when a valid dja

2019-08-26 07:00发布

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.

Here is my code :

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')

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.

1条回答
别忘想泡老子
2楼-- · 2019-08-26 07:40

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/topics/signals/

查看更多
登录 后发表回答